0
我想在帖子中加入表格模型id在評論中與評論ID匹配model.comment模型屬於帖子模型。在控制我的活動記錄查詢:在Rails中加入多個關聯
Post.joins(:comments)
它產生這樣
SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "post"."id"
查詢,但我不能選擇comments.How我可以生成類似下面
SELECT * FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "post"."id"
@dinnesh如果這個答案足以解決你的問題,請考慮接受它。 –
不,亞歷克斯它會生成這樣的查詢SELECT「posts」。* FROM「posts」 SELECT「comments」。* FROM「comments」WHERE「comments」。「post_id」IN(1,2,3)與SELECT「posts」相同。* FROM「posts」comments列未加載 –
這應該允許您通過帖子加載評論。 'Post.includes(:comments).first.comments'應該輸出第一篇文章的評論。 –