Class Central is learner-supported. When you buy through links on our site, we may earn an affiliate commission.


Active Record Associations in Rails Course

via Treehouse


Most of your Rails models are going to be connected to other models in some way. An Author has many Articles, and each Article belongs to an Author. A Doctor has many Patients, and a Patient may have many Doctors as well. Rails uses associations to make it easy to track these relationships in your database.

What you'll learn

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


Active Record Associations

Our blog app doesn't allow users to tell us how awesome our posts are. This situation is, of course, unacceptable. So we're going to need a comments system. We need to be able to associate comments with a particular post.

Chevron 11 steps
  • Introduction


  • Has Many Associations


  • Has Many

    1 objective

  • Association Methods

    1 objective

  • A "Has Many" Migration

    1 objective

  • Belongs To Associations


  • "Belongs To" Associations

    1 objective

  • Variations on Foreign Key Migrations


  • Has and Belongs to Many Associations


  • Has Many Through Associations


  • Has One Associations


Using Associations in Your App

Working with associated models in your controllers won't be much different from working with them in the Rails console. But rendering them in your views and representing the association in your routes might take a little more work. We'll cover those details and more in this stage.

Chevron 8 steps
  • Rendering Associated Models in Views


  • Rendering Associated Models Using Partials


  • Rendering Collections

    1 objective

  • Nested Routes and Resources


  • Nested Routes and Resources

    1 objective

  • Creating an Associated Record


  • Updating an Associated Record


  • Deleting an Associated Record


More Association Options

When it comes down to it, associations really just make some changes to the SQL queries that Active Record runs on your database. If you want, Rails gives you the power to take more control over those queries. In this stage, we'll look at some options for doing that.

Chevron 6 steps
  • Polymorphic Options


  • A Migration for a Polymorphic Association

    1 objective

  • Polymorphic "Has Many" Associations

    1 objective

  • Polymorphic "Belongs To" Associations

    1 objective

  • Destroying Dependent Models


  • More Association Methods


Taught by

Jay McGavren

Related Courses


Start your review of Active Record Associations in Rails Course

Never Stop Learning!

Get personalized course recommendations, track subjects and courses with reminders, and more.

Sign up for free