LoginSignup
5

More than 5 years have passed since last update.

has_manyの子要素が最低一つある親要素全てを選択する

Posted at

いつもやり方を忘れるので、忘れた時にぐぐったらこのページが出てくるように書いてます。

やり方

Post.joins(:comments).uniq

ちなみに生成されるSQLは

SELECT DISTINCT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id"

参考

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
5