I have below table structure. This is just an example
UserPost => user_id, post_id, Post, Comment
So, If I try to fetch all user_posts using the below query and do where on the comments table then it fires query for the comments table
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
So, is this the expected behavior or I am doing something wrong?
How to prevent the query for each user_post