joins

joins 的使用场合

过滤结果,而不是访问关联表的记录。

Post.joins(:comments).where(comments: {user_id: 1}).map { |post| post.title }
=> SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" WHERE "comments"."user_id" = ?  [["user_id", 1]]
……

阅读全文