LoginSignup
4
6

More than 3 years have passed since last update.

子テーブルの値を where 絞り込み Rails

Last updated at Posted at 2020-05-06

where句で値の絞り込み

Railsで子テーブルのカラム名で where 絞り込みを行う方法です。

User.joins(:posts).where(posts: {work_in_progress_flag: true})

上記の様に、
where句内で、ハッシュ形式で記述することで
子テーブルの値で絞り込みを行う事が出来ます。

where(子テーブル名: {カラム名: 値})

参考

Railsガイド Active Record クエリインターフェイス
https://railsguides.jp/active_record_querying.html#%E3%83%8F%E3%83%83%E3%82%B7%E3%83%A5%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%97%E3%81%9F%E6%9D%A1%E4%BB%B6

Railsガイドは大事

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