ActiveRecord - Rails Internals Series - II24 Mar 2016 ActiveRecord Rails
This is my second post of my Rails Internals series mainly focusing on ActiveRecord library and its modules.
The ‘Model’ component of Rails deals with business rules and business data that the users of an application work with. A web applications converts the business data into database value. In Rails, this conversion happens by using ActiveRecord library that implements ORM (Object Relational Mapping). Below diagram shows the translation of information from a user to database.
In general, ActiveRecord as a whole gives us various facilities like
- Persisting the data that can be retrieved from the database and rendered in view templates.
- Querying the database.
- Ensuring data integrity in database by applying validations on data coming from the outside world before it is being persisted.
- Defining relationship between two models and much more.
In context to Rails, Ruby class which inherits from the ActiveRecord::Base class is called Model. Base class sets up mapping between the model and table in the database. Also series of modules are included in Model class from ActiveRecord library.
Major Features of ActiveRecord
- Automatic mapping between given model class to certain database table and attributes of the class to columns of the table using migration.
- Storing each model object’s properties (data) as a row in the corresponding table.
- Model class doesn’t specify its attributes directly but infers from database schema to which it is linked. This technique is called reflection.
- Adding, removing, and changing attributes and their type is done directly in the database using migration file which will reflected in corresponding Model class.
- Active Record provides methods to perform CRUD operations (allows an app to read and manipulate data from database)
- Using accessors/attribute methods (getter and setter methods of the Model), column values of the database table can be set or retrieved
- Attribute query methods like @user.name? (where ‘@user’ is object of User Model and ‘name’ is one of attributes of User model) are created for a given model.
- Dynamic finder methods based on attributes of the model are provided. For example, ‘User’ model with attributes like ‘id’, ‘name’, ‘email’, and ‘admin’ will have following dynamic finder methods. User.find_by_name(“John Doe”) , User.find_by_name_and_email(“John Doe”, “email@example.com”) , User.find_by_name_and_id_and_admin(“John Doe”, 1, true).
- Validation, Association, Callback, Migration methods are available to Model.
Let’s visualize that when model objects are created in ActiveRecord context, they are born with their innate nature of how to work with database, validate their properties and so forth. These innate behaviours are added from series of modules like Core module, Persistence module, Relation module, Enum module, Association Module etc.. from Active Record library.
Once Model can be created and migrated to database, we can play with all methods added to our model using rails console. For example, in an application, let’s assume ‘User’ Model and ‘users’ table exist. To view all methods available for ‘User’ model, execute
User.methods.sort in rails console, to view only instance methods of the model, execute
User.instance_methods.sort, to view only class methods execute
(User.methods - User.instance_methods).sort.
Modules and their methods
Here I just listed few modules to get hang of them. We can’t remember or memorize every method from every module. So I attempted to blog tiny bit about ActiveRecord at least to have little awareness towards ‘under the hood’ of Rails. Hope you find this post useful.