Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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

やり方

Post.joins(:comments).uniq

ちなみに生成されるSQLは

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

参考

http://stackoverflow.com/questions/9922833/rails-how-to-get-objects-with-at-least-one-child

ironsand
思いついたアイデアをすぐに形にできる能力を身につけたいのでRailsを頑張って習得中。仕事ください。
http://programming.ironsand.net/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away