I have trouble understanding why are two duplicate sql queries generated in this situation:
I have a Post which has_many :comments
post = Post.where(id: 4).includes(:comments).first
generates these sql statements:
SELECT "posts".* FROM "posts" WHERE "posts"."id" = 4 LIMIT 1
SELECT "comments".* FROM "comments" WHERE "comments"."post_id" IN (4)
Now:
comment = post.comments.first
(no sql - good)
However:
post = comment.post
generates the same sql:
SELECT "posts".* FROM "posts" WHERE "posts"."id" = 4 LIMIT 1
It seems like the objects are not bound internally. Is there a way I can do that manually to avoid the second sql ?