DEV Community

Randy Steele
Randy Steele

Posted on • Updated on

Active Record Associations in Rails

Active Record is a Object Relational Mapping System. When thinking of the MVC design model it is the 'M' (Model) and it represents the data and logic. Active Record is very powerful but sometimes it can get tricky and confusing. I'm going to try to help with some of the confusion of Active Record Associations in rails.

First let's talk about what Active Record associations are implemented in rails. Currently, rails supports six types of associations. belongs_to has_one has_many has_many :through has_one :through has_and_belongs_to_many

The first couple of these are pretty easy to understand. belongs_to means that resource a belongs to resource b. An example of this is, if I had a music app, let's says a youtube like application. I have a track resource and a genre resource, My track would belongs_to the genre

Here's how I would set up that in my rails app.

class Track < ApplicationRecord
    belongs_to :genre 
end
Enter fullscreen mode Exit fullscreen mode
class Genre < ApplicationRecord
    has_many :tracks
end
Enter fullscreen mode Exit fullscreen mode

So, now my app has a Genre resource that has_many :tracks and a Track resource that belongs_to a genre

That's all great and all but what does this really mean and why does it matter? Let's check that out! I already have my seeds.rb file set up so if I run rake db:seed (make sure you don't have any pending migrations) and rails c to go into the console. I can check this out. Now that I'm in the console I can run Track.all and you will see that I have a genre_id associated with the Track automatically.

=> #<ActiveRecord::Relation [#<Track id: 1, title: "First Track", artist: "Randy", year: "2021", created_at: "2021-02-20 20:09:45.283145000 +0000", updated_at: "2021-02-20 20:09:45.283145000 +0000", genre_id: 1>]> 
Enter fullscreen mode Exit fullscreen mode

Now if I want to check what the genre id is for track#1 I can

2.6.1 :004 > Track.first.genre_id
  Track Load (0.4ms)  SELECT "tracks".* FROM "tracks" ORDER BY "tracks"."id" ASC LIMIT $1  [["LIMIT", 1]]
 => 1 
Enter fullscreen mode Exit fullscreen mode

But that's not all, I can also get all the tracks associated with a particular Genre like so:

2.6.1 :006 > Genre.first.tracks
  Genre Load (0.3ms)  SELECT "genres".* FROM "genres" ORDER BY "genres"."id" ASC LIMIT $1  [["LIMIT", 1]]
  Track Load (0.2ms)  SELECT "tracks".* FROM "tracks" WHERE "tracks"."genre_id" = $1 /* loading for inspect */ LIMIT $2  [["genre_id", 1], ["LIMIT", 11]]
 => #<ActiveRecord::Associations::CollectionProxy [#<Track id: 1, title: "First Track", artist: "Randy", year: "2021", created_at: "2021-02-20 20:09:45.283145000 +0000", updated_at: "2021-02-20 20:09:45.283145000 +0000", genre_id: 1>]> 
2.6.1 :007 > 
Enter fullscreen mode Exit fullscreen mode

This is all made possible because we set up our Active Record associations within each of our models.

This will be more important and useful once we get our app built out and we add more and more Tracks and Genres. Next week I will dive into the has_many_through association, which for me was one of the more difficult associations to process.

Here's a couple of resources if you need some additional help or just want to learn more:
Rails Guides
Rubynnrails.org

Top comments (0)