Active Model – the hidden gem behind Active Record
Active Model has been extracted from Active Record in Rails 3.0 (released in 2010). The primary goal of this extraction was to untie Active Record from Action Pack, to make controllers and views uncoupled from the persistence layer: the database. Thus, Active Model became a building block for the M in Rails MVC, covering both persistent and non-persistent models.
Let’s take a closer look at this Rails component and learn how it could be helpful beyond Active Record.
Active Model as an interface
Although usually invisible to developers, Active Model contributes a lot to Rails’ productivity. Many Rails helpers, such as #redirect_to
, #link_to
, and so on, rely on the Active Model interface. In Ruby, when we say interface, we imply duck typing.
If it quacks like a duck, then it must be a duck
Duck typing is a technique of writing code based on the assumption that an object responds to a given method without...