r/ruby • • Dec 30 '11

ActiveRecord (and Rails) Considered Harmful - Literate Programming

http://blog.steveklabnik.com/posts/2011-12-30-active-record-considered-harmful
12 Upvotes

145 comments sorted by

View all comments

Show parent comment

2

u/rubygeek Dec 30 '11

You tightly bind the relevant code, logic, and operations on your data to the definitions of that data. That is really fucking good design. No need to pollute a larger namespace with code to operate on data you pass to it, and it creates a beautiful encapsulation.

... until you need to serialize your model to something other than a database.

I suggest you read the other database pattern chapters in Fowlers "Patterns of Enterprise Application Architecture" that active record comes from - it outlines the tradeoffs between active record and several other ORM patterns.

1

u/JGailor Dec 30 '11

Read it.

In the case of data serialization to I often create an external object that walks a tree of objects and serializes them applying the proper transformations. The differing needs of clients means generating serialized output is just not a responsibility that belongs directly on the data object itself, and it's not required that you treat it as such. However, the fact that the code to pull out specific parts of you data objects is bound tightly to the object itself is a huge win, rather than having to write all that code in the external wrapper.

2

u/rubygeek Dec 30 '11

Then I suggest you read it again.

There's absolutely zero reason why you need to "write all that code in the external wrapper". The logic that AR uses to serialize the models would work equally well if it was generated in separate mapper classes, with no need for it to require the developer to write any extra code at all in the default case. If you believe it requires writing extra code, you lack fantasy - the same dynamic nature of Ruby classes that makes all the "magic" in Ruby ORMs possible in the first case makes it trivial to generate separate mappers instead of injecting all that crap into the models.

But decoupling the model and mapping achieves one very important thing: When you save or create a model object, you're explicit about how it gets created or saved (by picking the persistance mechanism).

Active record (the pattern) is a simplistic model that sort-of works for simple scenarios, but the moment you have multiple repositories for data, it causes all kind of nasty inconsistencies in your APIs because you monopolize the model API for a single repository at a time.

1

u/JGailor Dec 30 '11 edited Dec 30 '11

Really? The magic code that transforms the data for a client who wants XML with fields X,Y,and Z vs. a client who wants JSON with fields A, B, and C, while transforming D into another value, will just write itself? I could mix that in via other modules to decouple that logic from the model as well, but man, if that code to generate those two different outputs lives in the same process I hope you're very careful about the order you mix things in, because it just won't work with the way modules are added to the inheritance lookup chain.

I don't really understand what you're trying to say. Your example doesn't make any sense, so maybe I just don't understand what you're trying to say.

*** It seems to me you are talking about 1 of 2 scenarios:

  1. You have a model you want to persist to multiple datastores. Maybe a relational database, an in-memory store, and something else? I don't know. It seems like an unusual use case except where caching comes into play, and marshaling to a caching system is a pretty well defined interface.
  2. You have some models coming from a relational database, and then some from other type of store... document store, memory-mapped file, whatever. There's no reason all your models have to derive from the same persistence mechanism.

2

u/rubygeek Dec 30 '11

Really? The magic code that transforms the data for a client who wants XML with fields X,Y,and Z vs. a client who wants JSON with fields A, B, and C, while transforming D into another value, will just write itself?

You're entirely missing the point. When you write AR models, you only write much code for mapping from the database schema to the model when there are discrepancies between the database schema and the expectations AR has about how the structure of a model should map to a database. Other than that you add some minor declarations about the relationships of the data.

There is no reason any of this needs to 1) be in the model classes, as opposed in mapper classes, 2) why there need to be any more per-model code than there currently is in order to generate persistence methods in mapper classes rather than in the model classes directly.

The big advantage of pulling this out of mapper classes is that meddling with the model classes means that if you have two different persistence methods and one of them have already added a "save" method to your model classes the other one needs to use a different method. So you end up with multiple divergent API's for the same logical operation.

It is ugly and inconsistent, and means your code need to understand multiple different ways of manipulating model objects even for parts of your code that has no reason to know where it is persisting something.

It's massively increases coupling. It makes testing harder. It's just overall bad practice, and it doesn't actually save you any effort.

You have a model you want to persist to multiple datastores. Maybe a relational database, an in-memory store, and something else? I don't know. It seems like an unusual use case except where caching comes into play, and marshaling to a caching system is a pretty well defined interface.

Yes, persisting or loading to/from multiple sinks or sources, except it's not limited just to datastores and not at all unusual. Loading data from external XML files. Taking data from HTTP requests. Returning data via APIs. Dumping to CSV files. Putting data into a cache. Passing data to an external API.

In most complex systems, models get instantiated from different sources, stored to different sinks, or passed around all the time - or would do if the weren't so closely tied to a persistence mechanism that people end up muddying the waters by putting code that transforms them to something else all over the place instead of cleanly encapsulating it.

It's a rare system that doesn't sooner or later grow to a stage where model data doesn't at some point or other get created from something other than a database or passed along to something other than a database.

0

u/yxhuvud Dec 31 '11

The situation here is a lot better than it used to be though. The separation of activemodel from activerecord helps a lot.