I m 寻求帮助,使协会能够按要求负荷工作。
这是一个简化的例子。 我有3个模型:Post
,Comment
,User/code>。 参考资料:
参考Post
有许多评论和<代码>CommentUser
(作者)。 在我上台时,我期望看到邮局+所有评论(及其各自的作者姓名)。 这就需要以下两个问题:
select * from Post -- to get post data (1 row)
select * from Comment inner join User -- to get comment + usernames (N rows)
在法典中,我有:
Post.find(params[:id], :include => { :comments => [:author] }
但是,它不像预期的那样工作:正如我在后面看到的那样,N+1的点击仍然重新发生(其中一些点击)。 我如何能够优化这一努力?
UPD After some investigation, it looks like code was correct, but it doesn t work as expected in case I have named belongs_to in a Comment model. Once I changed from :author to :user, it worked as expected.