2
2

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 5 years have passed since last update.

関連モデルを使って検索をかける時

Last updated at Posted at 2018-02-05

関連モデルはwhereの検索に使えない!
User.where(id: 1..10)はできるけど
has_many関連のfavoriteモデルのidから検索したい時
User.where(favorites: 1)?(idはどう指定する?)
User.where(id: 1)? (これだとUserのidをとってしまう)

users = User.includes(:favorites).references(:favorites)
users.where(Favorite.aral_table[:id].eq(1))

idが1のFavoriteモデルと結ばれているUserを検索できる
他にも .not_eq()メソッドがある

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?