我有下表结构。这只是一个例子
UserPost => user_id, post_id, Post, Comment
因此,如果我尝试user_posts
使用以下查询获取所有内容并在comments
表上执行位置,那么它会触发对该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