2
1

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.

Rails includesした子テーブルの要素をpluckする

Last updated at Posted at 2021-04-21

検索しても、有るようで無かったのでメモがてらに記載しておく

Client.where(id: ids).pluck(:name)

みたいな書き方が一般的だが、シンボルを使うとincludes等を使った際に、どう記述してよいのか悩む時が有る。

Client.includes(:client_sheet).where('client_sheets.id in (?)', cs_ids).pluck('id', 'client_sheets.name')

その場合は、シンボルではなくて、文字列にして記載すればOK。その際は、親に対して子が1つであってもclient_sheetsの様に複数形にする必要が有るので注意。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?