Last week I pointed out the new ActiveRecord caching feature that lets you cache all ActiveRecord calls on a specific model:
1 2 3 4 |
User.cache do me = User.find(1) # DB hit again = User.find(1) # Cache hit end |
That’s great, but it only works on the model for which the cache block is applied. If you have a block of code that accesses multiple model classes, you’d have to either nest several cache blocks or this new top-level cache statement :
1 2 3 4 5 6 7 8 9 10 11 |
ActiveRecord::Base.cache do # DB hits me = User.find(1) companies = Company.find(:all) # Cache hits me = User.find(1) companies = Company.find(:all) end |
and all queries to any active record models within the block will be cached.
tags: rubyonrails, rails

Sorry for the tone, I’m just venting, but I think it needs to be said:
This is so wrong. Why does RailsCore keep insisting on throwing the baby out with the bath-water when it comes to solved problems? Use an IdentityMap. This stupid hash isn’t going to solve the problems with NestedSets
Node.find(:first).children.first.parentor a dozen other scenarios. AR needs object Unique-ing.RailsCore needs to stop reinventing the wheel at every opportunity. These patterns exist because they’re known solutions to problems. Leveraging a few isn’t going to bring down some sort of Java curse.
You’re assuming that an identity map wasn’t considered. That assumption would be incorrect.
An identity map is ALSO an interesting caching strategy, but it’s higher level and much more involved to both implement and utilize. A query cache is, quite simply, just that. A cache of queries, which decreases the interaction between app and db. Unlike an identity map, the implementation is trivial and the impact is completely below the surface.
But it seems that your emotions are more interesting than the facts here. Why are you so mad?
WRT: “mad”: Can’t a guy vent? ;)
Anyways, it’s just frustrating to see gimpy hacks make it into Rails. It doesn’t solve the real problem of object unique-ing.
It’d be nice to have no new features in AR in 2.0, and instead have a major refactoring so the community could get more involved. Much of AR is practically a single class at runtime relying too much on mixins, and not enough on composition, and it makes it very difficult to work with.
At the very least if we could stop adding technical debt with “solutions” like this so that the real problems can be addressed. Maybe there’s no time for an IdentityMap right now? Fine. Why not a plugin then? Why bring something into AR/trunk that will (hopefully) someday not too far off in the future be made completely obsolete?
Ticket 428 will be forever burned into my mind. ;) Opened 2 years ago. Last modified 6 months ago. Mixed in a bunch of nasty hacks. I wrote an adapter that simply did away with it all (
def active?; true end) and still managed to outperform by a decent margin. But working around that cruft is a PITA. I’d rather we not add more.Maybe that’s just me though…
Who says we’re trying to solve the “problem” of object unique-ing?
Regardless, your tone is very poor conductor for collaboration. If you have a big interest in implementing an identity map approach, no one is stopping you. Please Do Investigate.
I’m a big believer in “worse is better”, though. I’d much rather have a 80% solution today than a potential 100% solution at in some possible future.
And I usually don’t hold my breath for folks with with a discussion pitch higher than the number of their contributions to date.
I’d welcome a positive surprise, though.
It’s a problem typically solved by a unique-ing mechanism was all I meant to imply. Focusing on extra queries is missing the issue I think.
You’re right about the tone. I could’ve been more diplomatic. I’m sure you of all people can appreciate that controversy has it’s advantages though. :)
I’m not saying that Rails isn’t the best thing since sliced bread, because it is. I’m not saying that ActiveRecord isn’t great, because it is. A few too many holy-cows were slaughtered on the way though. It was probably inevitable, but in a very Fowler-esque sense, AR is in serious need of some slash & burn refactoring to the internals.
...and if you’re open to some of that, then yeah, I’d be happy to pitch in.
BTW, questioning my open-source pedigree? Not very nice. :)
Great dialog guys – there’s nothing like passion to spice up a conversation!
About ‘spicing up’... maybe we should remind ourselves what went/goes on in PHP lists… :-)