It seems to me that fragment caching and eager loading are -- at least sometimes -- somewhat at odds with each other. Let's say I have a User who has many posts which each has many comments which in turn also can have many comments and so on.
When I have to render the page, I could choose to do eager loading of the user, all her posts, all their comments and so on, in order to avoid hitting the database n-1 times. Or I could load each object lazily and rely on fragment caching to only query the database for objects that are new or have been changed. Using both fragment caching and eager loading seems to be wasteful, as I would potentially do a very complex query and instantiate a lot of objects only to use a small part of them.
But what if I have an application in which a User has many Foos which in turn has many Bars and so on, but in which each Foo is created complete with all its Bars and their associated objects at the same time and from then on never changes. In that case, I would like to use fragment caching for Foos which have been rendered, but to use eager loading when I have to load a new Foo with all its associated objects. After all, there's nothing to be gained from caching fragments at a more granular level.
What is the best way in Rails to accomplish this? I suppose I could do one query to get just the ids of the Foo, and then do an explicit find with eager loading when I have to render each Foo. Is there a better/more elegant/more idiomatic way of doing this?