Hey, thanks for commenting! The #includes method does not accept filtering AFAIK, the where clause would be applied to the query to fetch the posts but not to the loaded comments. So when I paste that query on the console I get the following:
irb>Post.includes(:comments).where(comments: Comment.where(censored: false)) Post Load (2.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" IN (SELECT "comments"."post_id" FROM "comments" WHERE "comments"."censored" = $1) LIMIT $2 [["censored", false], ["LIMIT", 11]]
Comment Load (0.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" IN ($1, $2, $3, $4) [["post_id", 2], ["post_id", 3], ["post_id", 4], ["post_id", 5]]
Then if I add a method on the Post model to get uncensored comments:
Yeah, you can always do the queries straight somewhere else, but that wouldn't help you preload the records. The example was very simple I hope it doesn't miss the point, which is preloading and not doing extra queries and not just being able to get the data. Maybe something like this paints a clearer picture:
This creates the same N + 2 queries as before since the .includes(:comments) it's actually useless here. Sure you can always find a way around not having associations in the model, one of the ways to do so and that I didn't mention in the post is to do the following:
Hey, thanks for commenting! The
#includes
method does not accept filtering AFAIK, the where clause would be applied to the query to fetch the posts but not to the loaded comments. So when I paste that query on the console I get the following:Then if I add a method on the
Post
model to get uncensored comments:And run the same example I did in the post:
We go back the N + 2 query problem
I think the code you suggested would be a nice way of getting posts with comments, but it does not help preload custom queries.
Thanks, I didn't think this through. Or at all, with this weird
#includes
;)But still, you can do it without association in the model.
Yeah, you can always do the queries straight somewhere else, but that wouldn't help you preload the records. The example was very simple I hope it doesn't miss the point, which is preloading and not doing extra queries and not just being able to get the data. Maybe something like this paints a clearer picture:
This creates the same N + 2 queries as before since the
.includes(:comments)
it's actually useless here. Sure you can always find a way around not having associations in the model, one of the ways to do so and that I didn't mention in the post is to do the following:But that, IMO, it's very ugly and does not follow OOP at all.