Active Record Association Types

Nick Christensen
4 min readOct 6, 2021

In today’s blog we’ll be discussing some of the common types of active record associations and how to use them. Those associations are; belongs_to, has_one, has_many, has_many through: and has_one through:. Each of these associations allow models and databases to communicate together using their primary-key and foreign-key relationships. By using these associations active record allows us to easily access instanced data shared across models.

Here, take this! It belong_to :you!

The belongs_to association links a model with another model. What that means is that every instance made in the model with the belongs_to declaration, will be able to communicate with the model that the declared model belongs to. Let’s use this example to try and make this a bit easier to understand. Say you’re creating a food app and it includes chefs and recipes. Each recipe needs to be assigned to exactly one chef. What would the model look like?

class Recipe < ApplicationRecord
belongs_to :chefs
end
Here is an example of our table with the Primary and Foreign key pairs.

Please note that belongs_to will only create a one-to-one connection. In the example above each recipe only knows about its chef. The chef doesn’t know about the recipe. To do that we need to create a bi-directional association. We can do that with our next associations!

Can I please has_one :cheeseburger? Wait! I’ll has_many :cheeseburgers please!

The has_one and has_many associations are very similar. You’ll most likely see these attached to the corresponding belongs_to model. The has_one association has a one to one relationship. So if you refer back to our food app, we had the Recipe model with a belongs_to :chef association. Well, if our chef only had one recipe, our chef model might look something like this:

class Chef < ApplicationRecord
has_one :recipe
end

For our particular app that’s not going to be very effective. Most chefs have many recipes and if someone is out there claiming to be a chef with one recipe, it better be the best thing anyone has ever tasted! How do we solve this problem? With a has_many association! In most cases this is what you’ll want to use over the has_one association. The has_many association will account for zero or more instances, so lets use that for our Chef model too!

class Chef < ApplicationRecord
has_many :recipes
end

Please note that it is important that you now use the plural of recipe for the has_many association.

Too many cooks in the kitchen!

Now that our chef and our chefs recipes can communicate with each other, we need our chef and their recipes to be able to communicate with the cooks. We’ll be doing that with a has_one :through and a has_many :through association.

The link between the chef and cooks table is our recipes table. Our chef needs to go through the recipes to talk to the cooks and our cooks need to go through the recipes to talk to the chef. First lets modify our Recipe model by adding another belongs_to.

class Recipe < ApplicationRecord
belongs_to :chef
belongs_to :cooks
end

Now lets modify our Chef model to set up our first connect between our chef and the cooks.

class Chef < ApplicationRecord
has_many :recipes
has_many :cooks, through: :recipes
end

As you can probably see, this is pretty straight forward. Our chef has many cooks and in order to talk to them they have to go through the join table which is recipes. All we have left to do is create our Cook model!

class Cook < ApplicationRecord
has_many :recipes
has_one :chef, through: :recipes
end

For this one we used the has_one association. The cooks are only going to answer to one chef, but they still need to go through recipes to do that. Please note here that I used the singular version of chef for the has_one association. This is very important and your code is likely not to work if you don’t pay attention to it.

Another example of our Primary and Foreign key pairs.

That’s it! Your chef should be able to look at all their recipes, delegate the cooks working on them and your cooks should be able to read all the recipes created by the cook along with taking on different recipes the cook gives them.

I really hope this has given you a clear understanding of active record associations and I’ll see you next time!

--

--