0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

RubyOnRails findの使い方

Posted at

#findメソッドとは
主なイメージは、「主キー(id)を指定して、見つかった1レコードを返す」です。

特徴
find()の()内に記述できる数値(id)のみ
idは主キーなので、1レコードのみ取得
検索に該当するデータがなかった場合、ActiveRecord::RecordNotFoundを返す

記述の仕方
モデル名.find(主キー)
例)
前提条件:sample_appのListテーブルに格納されているデータを取得したい場合

①id=1のみを取得したい場合

List.find(1)

②URLの:idを取得したい場合、

List.find(params[:id])
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?