【发布时间】:2021-04-17 01:53:16
【问题描述】:
我有下表结构。这只是一个例子
UserPost => user_id, post_id, Post, Comment
所以,如果我尝试使用以下查询获取所有 user_posts 并在 comments 表上执行 where 则它会触发对 comments 表的查询
user_posts = UserPost.includes(post: :comments)
user_posts.each do |up|
post = up.post # No Query
comments = up.comments # No query
comments_with_condition = up.comments.where(visibility: true).order(position: :asc).first.data # Fires query for .where and .order as well.
end
那么,这是预期的行为还是我做错了什么?
如何防止查询每个user_post
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-6.1