This short article covers some steps you need to implement to get your own ActiveRecord implementation.
Sample ActiveModel looks like this:
First trouble is that ActiveModel does not provide any attribute access API. Or I did not google enough. So we need to create our own!
Let us have an instance variable @attributes where we will store our model’ data. We need to define getter and setter methods for all the attributes of our model. This may be done with the attr_accessor method. But when user sets the value for some attribute, we should store that in our @attributes variable. And here is the first step to our black magic: we will override the attr_accessor method.
I just wrapped the code into a single module. Remember: when you include the module in a class, all the module’ methods become class methods.
Now we will include this module before attr_accessor calls. But beware: you need to declare an @attributes instance variable in the constructor!
And let’s just agree with the following convention: all our attribute names should be symbols.
Now, we can implement our constructor. We now have all the attributes’ getters and setter and thus we can simply call them in our constructor:
Now let’s implement some basic model persisting. First, we should not forget about our validations and add valid? test to the save method.
Let’s say our save method should return the model instance. Thus, we should put the model’ data into the database and get the id for that data (if we put the data with the INSERT statement).
So there is an important caveat: in order to get the correct model id, you need to get it from database in the same transaction as the update/insert statement. The mysql2 gem does support multiple query statements in a single transaction. But to perform such a query, you will need to set the MULTI_STATEMENTS flag when creating a Mysql2::Connection instance.
Here I used the instance variable @connection to make it available within the rescue and ensure statements.
Now we will use our instance variable, @attributes to create an SQL query:
I used the ActionController::Base.helpers.sanitize helper method to escape the query parameters.
Now we should simply wrap our query into a transaction and get an id from the database.
Quite big method, sure. Yet, it performs all the UPDATEs and INSERTs for us.
Let’s add some attribute with the default value, created_at and check how the whole class works: