Active Record associations in Rails
Introduction
Active Record has many power features, and association is one of them. Using association between models in the application makes your code more simpler and easier to maintain and understand. To use such a powerful feature, you have to get deep understanding of what exactly ActiveRecord association does. Through this post, I will try to illustrate the different types of associations by cartoon which will make it easy for you to implement the concept in any application. so, let’s get start.
Why using Association
Association is a connection between two Active Record models, so they make common operations simpler and easier in your code. They are a set of macro-like class methods for tying objects together through foreign keys. They express relationships like “Project has one Project Manager” or “Project belongs to a Portfolio”. Each macro adds a number of methods to the class which are specialized according to the collection or association symbol and the options hash — api.rubyonrails.
The simplest association (one-to-one)
This relation is the easiest and simplest type of association, where one element is belong to another one element. For example, you have one unique driving license card and that driving license belongs to you — you can’t have multiple driving licenses. Most probably, you will use this type of association rarely because it doesn’t do much to your application. Often times you will need to use the other different types of association which I will demonstrate in below.
More complex association (one-to-many)
Suppose that you have two cute kids “Mary” and “Sophie”. Then you have many kids. From the other hands, “Mary” and “Sophie” belongs to you. Congratulations, this is the second type of ActiveRecord association.
Active Record uses macros to apply the association relationships; however, to be able to use these macros you should build migration tables. In our case, we have two tables father’s table and kids’ table. In kids’ table we need the father id to declare that those kids are to the actual father. So what you have to do is to put a foreign key into kids table with “father_id”.
Top Tip: the foreign key always goes to the table that has belongs_to macro.
Rich-Complex associations (many-to-many)
This is the most complex one but it is more versatile and gives you a lot of flexibility to control you models. To understand what is happening, let’s simplify the relation to something happening in our daily life by taking the taxi-passenger model as an example.
What happens is that a passenger can have multiple rides, so he probably will have multiple taxis for his rides. If we have multiple passengers, how we can set the association? To know the right association, ask yourself what is the common between taxis and passengers? That’s right rides, if you checkout this Venn diagram below you can see that rides are common which is the perfect case for inner join table.
As you can see the rides are the common with passengers and taxis, so rides will be our joining table. Here has_many through /has_and_belongs_to_many macro comes in handy, those two macros will add methods of both passenges and taxis model to rides table. They are the same in functionality but different in how to implement them — you can read more about them here.
Steps to implement the has many through association.
- Set the association to all the model parts to get full model accessibility.
- Set data for the joining table.
Remember that: the model_id always goes to the belongs_to table.
Now, you have set all the connections between your models. As a result models can see/access each other with min code.
Conclusion
Association in active record has a powerful features, you will get sense of it when designing a more advance rails app with nested resources. Using complex association will save you a huge amount of time. Hope this post gave you a better understanding of what Active Record association are. You can follow me on twitter at @salmaeng 😉