Raul Chedrese

Jorge Manrubia Explains the Benefits of Active Record

I’m enjoying the newish 37 Signals dev blog. It’s great to see them writing about the patterns and approaches they use especially now that many of them have fallen out of favour.
The distinctive trait of the Active Record pattern is combining domain logic and persistence in the same class, and that’s how we use it here at 37signals. At first glance, this might not look like a good idea. Shouldn’t we keep separated things, well, separated? Even Fowler mentions that the pattern is a good choice for domain logic that isn’t too complex. It’s our experience, however, that Rails’ Active Record keeps code elegant and maintainable in large and complex codebases. In this article, I will try to articulate why.
In my experience the most impactful downside of Active Record is the lack of flexibility. Active Record models are fused to Rails and your database. If you are ok with that then party on but if you turn out to be wrong you’ll be re-writing all of your model code.
The caveat here is that Active Record requires you to control the database schema to leverage what it offers fully. Assuming this is the case, the ability to seamlessly persist rich and complex object models is key to making the pattern work in large and complex codebases.
Jorge nails one of the big tradeoffs here. In large and complex codebases you may not have the ability to control the database schema. Changing the schema may have far reaching consequences or just take a long time due to cross team dependencies.
In a database-powered application, domain logic is indissolubly linked to persistence.
This is the part I don’t agree with. It is possible to separate your domain logic and persistence logic, as with everything, it comes with tradeoffs.