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

Treehouse

Active Record Associations in Rails Course (How To)

via Treehouse

Overview

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

Syllabus

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

    2:52

  • Has Many Associations

    8:53

  • Has Many

    1 objective

  • Association Methods

    1 objective

  • A "Has Many" Migration

    1 objective

  • Belongs To Associations

    2:20

  • "Belongs To" Associations

    1 objective

  • Variations on Foreign Key Migrations

    6:31

  • Has and Belongs to Many Associations

    6:32

  • Has Many Through Associations

    7:26

  • Has One Associations

    3:39

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

    3:15

  • Rendering Associated Models Using Partials

    4:29

  • Rendering Collections

    1 objective

  • Nested Routes and Resources

    4:31

  • Nested Routes and Resources

    1 objective

  • Creating an Associated Record

    8:41

  • Updating an Associated Record

    6:40

  • Deleting an Associated Record

    3:04

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

    7:37

  • A Migration for a Polymorphic Association

    1 objective

  • Polymorphic "Has Many" Associations

    1 objective

  • Polymorphic "Belongs To" Associations

    1 objective

  • Destroying Dependent Models

    1:27

  • More Association Methods

    3:46

Taught by

Jay McGavren

Reviews

Start your review of Active Record Associations in Rails Course (How To)

Never Stop Learning.

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

Someone learning on their laptop while sitting on the floor.