ActiveRecord model responsibilities

While I’m drafting this post, I got pointed to this article, Fear Of The Class: Fat Model Kills Your Software Design, from a tweet. This article says 90% of the things I wanted to say here, so I’ll not repeat those.

One extra thing I want to point out is some madness I faced in recent weeks. And of course, it relates to the AR model responsibilities topic.

In Rails land, I accept the fact we can call to_json on a AR model instance and pump the output back as a JSON response. I occasionally use it when I’m just prototyping something, but I seriously believe calling to_json on AR model is wrong. to_json simply isn’t a responsibility to an AR model class. Handle it in the views with RABL, use a decorator/presenter, freedom is all yours!

As I dislike Rails giving all AR model to_json method, I’m hit by something much worse. In a central ORM lib at work, a recent change was made to automatically serialise boolean fields to JSON format upon data retrieval. So if someone needs just a true or false … too bad, ORM gives back JSON true and JSON false, convert it back to boolean yourself!!! When I read and noticed the change, it was a WTF moment. When I questioned the change and told to go and read on it, I wave white flag …

Published: 2012-08-24
blog comments powered by Disqus