Rails 4 brought us ActiveModel::Model
. It provides a light weight interface that’s similar to an ActiveRecord::Base
model.
for example, I can create a Person class like so.
1 2 3 4 5 6 7 8 9 10 |
|
1 2 3 4 5 6 7 8 |
|
This is great for instances where you don’t need a full database backed Active Record model. I’ve used them for form objects and in controllers where I have complex logic.
You can think of these as higher level abstractions above your ActiveRecord classes. Also, be conscious of the dependancy direction. An ActiveModel model can depend on an ActiveRecord model but your ActiveRecord models shouldn’t depend on an ActiveModel model.
Here’s a more involved example.
Lets say I have 2 ActiveRecord classes Org
and User
1 2 3 |
|
1 2 3 |
|
Now I’ll create an ActiveModel model (non database)
Notice the validates_each method… Its going to check each of the ActiveRecord objects and let them raise up any errors to the Signup class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
Awesome right!!
So why do all this? Well, the single responsibilty states that every class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class. ActiveRecord is responsible for persistence to the database. This will keep our classes with a narrow focus and allow us to refactor and create more use cases in the future. I think it’s a win. I find this strategy is generally good for one directional workflows such as signup or in a shopping app cart checkout.