Skip to content

Instantly share code, notes, and snippets.

@6temes
Created December 7, 2016 06:56
Show Gist options
  • Save 6temes/f8959a0ebc2a91ca76a328c7f5d31c0c to your computer and use it in GitHub Desktop.
Save 6temes/f8959a0ebc2a91ca76a328c7f5d31c0c to your computer and use it in GitHub Desktop.
The story of ActiveModel

ActiveModel

ActiveModel useful tool that some developers tend to overlook when getting started developing with Rails but that, well used, can add a lot of clarity and convenience to the code in some cases.

The use case of ActiveModel to have a class that behave like an ActiveRecord model, but without DB persistence. That allows, just by importing ActiveModel into a regular Ruby class, to get a lot of useful features that usually belong only to Rails models. These features include validation, serialization, callbacks, attribute methods, etc.

ActiveModel is, somehow, still a bit obscure part of Rails, but the official Rails Guide has some useful examples where we can get a glimpse of how poweful it is.

The story of ActiveModel is interesting as well. Back in 2008 there was an important schism of Rails 2 when Ezra Zygmuntowicz and Yehuda Katz developed Merb. One of the main goals of Merb was to be developed with modularity in mind; in contast to the monolithic design of Rails 2.

The ideas behind the design of Merb were good and the framework started to gain traction among the Ruby community. DHH realized that this division in the community was not going to be good for anyone and he made the effort to try to fix the situation by convincing Ezra and Yehuda to join the effort and introduce their ideas to the development of Rails. As a result of that, Rails 3 was born carrying many innovations from Merb. I think that this is a nice story that shows how people can set aside their own egos, in order to achieve a greater good. はっぴいえんど!!

So, what about ActiveModel? Well, one of problems of ActiveRecod before Rails 3 was that the database access methods where tightly coupled with the methods that corresponded to the business logic. The innovation of ActiveRecord 3 was to separate those methods in two different subcomponents. And that’s how ActiveModel was born!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment