An important topic in any application that tries to solve a problem are associations. Objects, items, things are connected in many ways with each other. And those connections are what today’s blog post will be about. It will show how to create and use basic associations in Rails.
As my next step in Junior Rails Developer Course, I was asked to create a simple app that will teach me how to use associations in Rails. In order to do that, I have created two models that represent employees in a company and various departments in it.
To create an association in Rails you need to do two things. Add proper method to both or one of the connected models. And then create a proper migration. In my app, I have decided that each Employee belongs to one of existing departments. Department has many employees, but also to make things more interesting each department belongs to the manager.
Belongs to is the simplest association which tells us that one object belongs to another one. For example, book belongs to an author or in our case each employee belongs to one of existing departments.
class Employee < ActiveRecord::Base belongs_to :department end
Another usage of belongs to was a bit trickier. I wanted to make each department to have one manager. But since I didn’t want to create a separate model for that because a manager is in fact on of department’s employees I needed to do it differently. Fortunately belongs_to method in Rails supports many additional options. One of which I used in that example. Rest of them you can check out in Rails Guides 4.1.2. To make manager work I used class_name.
Class_name helps when model name can’t be figured out using association name. For example in our case association name was manager. But in our app manager is, in fact, an object of Employee class. Using class_name we can tell Rails that even though our association name is manager, the model that is actually used is called Employee.
class Department < ActiveRecord::Base belongs_to :manager, class_name: "Employee" end
Another type of association used in my project is has_many. Which basically tells us that object of our class has a connection with many objects of another class. In that case, a department has many employees. Usage of that is fairly simple and you can see it in the code below.
class Department < ActiveRecord::Base has_many :employees end
Last part that is required to make it work is to create proper migrations. That part of making proper associations is fairly simple. We create proper fields in the database for those tables that correspond to a model that has a belongs_to association in it. So in our case, we need to create the department_id field in employees table and manager_id in departments table.
class AddReferences < ActiveRecord::Migration def change change_table :departments do |t| t.integer :manager_id, index: true end change_table :employees do |t| t.integer :department_id, index:true end end end
There was one problem with a manager when I started to make my app work. I couldn’t figure out whether the department should belong to the manager or the other way. Fortunately, guys from Arkency helped me with that and explained that if I would put belongs_to in Employee I would need to create field managed_department_id. But since not all employees are managers many of them would have nil in it. It should be the department that has the manger_id field in it and because of that belongs_to should be in department model not in employee model.