LoginSignup
4
4

More than 5 years have passed since last update.

ActiveRecordでincludesとwhereを同時に使う時はJOINするテーブル名の指定に気をつける

Last updated at Posted at 2014-10-28
Post.includes(:comments).where("comments.content like '%いいね%'")

というようにincludesとwhereを合わせて使うと「文字列中のテーブル名を自動認識しなくする予定だからcomments.contentのテーブル名はreferences(:comments)で指定してね」というDEPRECATION WARNINGが出ます。

とはいえ文字列で条件を指定しているのが良くないので

Post.includes(:comments).where(Comment.arel_table[:content].matches('%いいね%'))

という感じでArelを使った方が良いです。ちょっとハマった。

元記事:ActiveRecordでincludesとwhereを同時に使う時はJOINするテーブル名の指定に気をつける | 江の島CodeStyle

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