I'm eager loading a model object with its associations:
user= User.includes(:posts).find(1)
But then at certain points in the code I would like to do something like this:
user.posts.where(:topic => "x")
But that just re-runs the query again. So instead I thought I'd do this:
user.posts.select{|post| post.topic == "x" }
That doesn't re-run the query. But I have a couple of questions.
First, Is this the right way to do it?
Second, I'm a bit confused about what select does in this case. Because when I run the last line even when I haven't used the includes function, the first time it runs the query and then after that if I run it again, it doesn't .. so is there some sort of caching involved? Because when i use the where clause it runs the query every single time.
Thank you.