LoginSignup
4
4

More than 5 years have passed since last update.

findで指定カラムの値のみを取得

Posted at

モデルのカラムの値一つだけが知りたいとき、findでselectオプションを使えば指定のカラムの値だけを取得することができる。

例えば、idが1のUserモデルについてusernameカラムの値だけが知りたい時、以下のように記述する。

User.find(1, select: "username")

SQL

SELECT username FROM `users` WHERE `users`.`id` = 1

参考サイト

4
4
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
4
4