Deeply Understanding Active Record Associations the Ruby on Rails Way

The purpose of this blog post is to describe Ruby on Rails associations deeply.

Setting up the Practice Environment

First off, I’ll create a new application for practice. I do this on the desktop as it’s an easy place to work from:

rails new association_practice

Next step is to change directories so I’m on the root level of the new application.

cd association_practice

Next, I’ll create an association to practice with. I’m going to start with planet Earth. With Earth as a starting example, I can make associations with most Earthly things.

rails g model Planet name:string

This will generate files for the migration, model, tests and fixtures. I added two planets to the fixture file and a simple test. Which you can see in the following screenshot:

Now that we have the Planet object in our system, I’m going to add the next level of association, the Continent:

rails generate model Continent name:string

This command orders the Rails app to generate four more files. I’ve added to them a little and you can see the changes in the following screenshot:

Ruby on Rails Associations

The goal of this section will be to clearly explain how to use associations to organize your web application’s data.¬† The association documentation on the Ruby on Rails guides is a good start, but my descriptions below I hope will help deepen your understanding of associations and how to use them.

Understanding belongs_to and has_many Associations

Continents and Planets

Let us create our first association between the continents and the planets. Each continent can belong to a single planet, but a planet can have many continents. Here’s how we assemble it:

Next we migrate the database.

rails db:migrate

Next we load the fixture data.

rails db:fixtures:load

Awesome. Now we have our fixture data loaded and our associations should be set. Here is a screen shot of the actions we can now take in the rails console.

rails c

  1. Here we search the database for a planet with the name Earth. That gives us an array with the matching result in it. We use the .first method to pull the first object. We assign this object to the variable name earth.
  2. Here we use our earth variable to get the name string data which contains the name of the planet (“Earth”).
  3. Here we list the continents associated with earth.
  4. Here we set antarctica variable to equal the continent object of our first continent, Antarctica.
  5. Here we ask the database, “What planet is associated with Antarctica?” It returns us the earth planet object. Notice that the Planet earth object has the same id at the bottom as the Earth at the top.

Work in Progress

I’m thinking of better ways to describe associations. I plan to deep my understanding of the following associations in the future:

  • has_one Association
  • has_many :through Association
    • I’m thinking of describing this by using International Organizations
  • has_one :through Association
  • has_and_belongs_to_many Association
    • I’m thinking of describing this by using members of NATO
  • Choosing Between belongs_to and has_one
  • Choosing Between has_many :through and has_and_belongs_to_many
  • Polymorphic Associations
  • Self Joins

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.