Skip to content
Menu
Railshero
  • Blog
  • About Me
  • Contact Me
  • Privacy Policy
Railshero
railshero.pw active record associations explained

ActiveRecord Associations Explained

Posted on June 26, 2023June 30, 2023 by Rails Hero

As a Rails developer, you’re likely familiar with ActiveRecord, a core Rails component functioning as an interface for data stored in a database. ActiveRecord’s power lies in its ability to establish associations between models, simplifying complex database operations execution. Let’s take a comprehensive tour of ActiveRecord associations, their various types, and their implementation in Rails applications.

Table of Contents

  1. Understanding ActiveRecord
  2. Associations in ActiveRecord
  3. The Basics: belongs_to and has_one
  4. has_many Association
  5. has_many :through Association
  6. has_one :through Association
  7. has_and_belongs_to_many Association
  8. Polymorphic Associations
  9. Conclusion

Understanding ActiveRecord

ActiveRecord, representing the Model in Rails’ MVC (Model-View-Controller) structure, is an ORM (Object-Relational Mapping) layer. It simplifies your application’s interaction with your database, transforming database rows into application-compatible objects.

ActiveRecord furnishes an elaborate API for performing CRUD (Create, Read, Update, Delete) operations, running queries, managing transactions, and executing diverse database tasks. This ability helps Rails to veil much of the involved SQL, allowing you to operate at a higher abstraction level.

Associations in ActiveRecord

At its heart, an ActiveRecord association is a way to express a relationship between two different models. Associations allow us to connect models together and to create complex queries and operations on those connected models.

In ActiveRecord, there are six main types of associations:

  • belongs_to
  • has_one
  • has_many
  • has_many :through
  • has_one :through
  • has_and_belongs_to_many

We’ll discuss each in detail, but first, let’s start with the two most basic ones: belongs_to and has_one.

The Basics: belongs_to and has_one Association

These two associations represent the simplest relationships: one-to-one connections. If you have a User model and a Profile model, and every user has exactly one profile, you might use these two associations like so:

class User < ActiveRecord::Base
has_one :profile
end

class Profile < ActiveRecord::Base
belongs_to :user
end

In this setup, the Profile model would have a user_id column in the database. The has_one and belongs_to associations create a series of methods that let you easily access the associated objects. For example, you could access a user’s profile with user.profile, and the user associated with a profile with profile.user.

has_many Association

The has_many association is used when one object can be associated with more than one of another object. For example, if a User can have multiple Post objects, you would set up the association like so:

class User < ActiveRecord::Base
has_many :posts
end

class Post < ActiveRecord::Base
belongs_to :user
end

With this setup, you could retrieve all the posts associated with a user with user.posts, and find the user associated with a post with post.user.

has_many :through Association

The has_many :through association is a more complex relationship, allowing you to associate objects through a third model. This is commonly used in many-to-many relationships. For example, consider a User, a Book, and a Review model. A user can write many reviews, and a book can have many reviews, but each review is associated with one user and one book. This could be set up like so:

class User < ActiveRecord::Base
has_many :reviews
has_many :books, through: :reviews
end

class Review < ActiveRecord::Base
belongs_to :user
belongs_to :book
end

class Book < ActiveRecord::Base
has_many :reviews
has_many :users, through: :reviews
end

Here, Review is the join model that connects User and Book. You can get all the books a user has reviewed with user.books, and all the users who have reviewed a book with book.users.

has_one :through Association

The has_one :through association allows you to set up a one-to-one connection with another model through a third model. Consider a Supplier, Account, and AccountHistory model. An account belongs to a supplier, and each account has one account history. We could represent this like so:

class Supplier < ActiveRecord::Base
has_one :account
has_one :account_history, through: :account
end

class Account < ActiveRecord::Base
belongs_to :supplier
has_one :account_history
end

class AccountHistory < ActiveRecord::Base
belongs_to :account
end

With this setup, you could get the account history for a supplier with supplier.account_history.

has_and_belongs_to_many Association

The has_and_belongs_to_many (HABTM) association is used to create a direct many-to-many connection between models without the need for a join model. This can be useful for simple relationships where the join model would not contain any additional information. For example, a Book can have many Authors, and an Author can write many Books:

class Book < ActiveRecord::Base
has_and_belongs_to_many :authors
end

class Author < ActiveRecord::Base
has_and_belongs_to_many :books
end

This setup requires a join table, typically named after the two models it’s joining (e.g., authors_books). You can find all authors of a book with book.authors, and all books written by an author with author.books.

Polymorphic Associations

Polymorphic associations allow a model to belong to more than one other type of model using a single association. For instance, you might have Image, Video, and Comment models, where an image or a video can have many comments, and a comment belongs to either an image or a video. This could be set up as follows:

class Comment < ActiveRecord::Base
belongs_to :commentable, polymorphic: true
end

class Image < ActiveRecord::Base
has_many :comments, as: :commentable
end

class Video < ActiveRecord::Base
has_many :comments, as: :commentable
end

With this setup, you could get all comments on an image with image.comments, or on a video with video.comments.

More Information on ActiveRecord associations: https://guides.rubyonrails.org/association_basics.html

Conclusion

Understanding ActiveRecord associations is key to becoming proficient with Rails and building complex applications with interrelated data models. While this guide has provided an introduction to each type of association, there’s still much to learn, including options like :dependent, :foreign_key, :class_name, :source, and more. As you grow as a Rails developer, you’ll find yourself reaching for these tools often, so it’s worth the effort to understand them deeply.

Remember, the best way to learn is by doing. So don’t just read this guide – create a new Rails app and try implementing these associations for yourself. Happy coding!

Leave a Reply Cancel reply

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

Tags

active record active storage assets assign to many associations attachment attachments authentication authorization avatar bootstrap cdn config database deploy deployments devise DRY environment variables file uploads Gemfile gems has_many helpers heroku index indexing initializer javascript pagination parameters postgres production public routes.rb ruby gem ruby gems search sendgrid server smtp stylesheets variants views voting

Recent Posts

  • Understanding the DRY Principle in Rails
  • Building Multi-tenant Applications with Rails
  • Rails Basics: Templating Engines
  • Deploying With Capistrano
  • Automated Testing in Rails

Archives

  • July 2023
  • June 2023
  • October 2021
  • September 2021
  • August 2021
  • July 2021

Categories

  • Active Record
  • Activity Logging
  • Apps
  • Assets
  • Attachments
  • Audio
  • Authentication
  • Authorization
  • Deployments
  • Error Pages
  • File Uploads
  • General
  • Heroku
  • Heroku
  • Pagination
  • Rails Basics
  • RubyGems
  • Search Engine Optimization
  • Search/Indexing
  • Testing
  • User Interface
  • Video
  • Views & Templating
  • Voting
  • Web Security
©2025 Railshero | Theme: Wordly by SuperbThemes
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT