Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Has Many Through Associations7:26 with Jay McGavren
Here we have a new app with Subscriber and Magazine models. We want people to be able to subscribe to multiple magazines, and we want to be able to look up all of a magazine's subscribers. We could do this through a has_and_belongs_to_many association. But what if we want to track additional info regarding the subscription itself, like how many months a person is subscribed to a magazine for? has_and_belongs_to_many associations can't do that sort of thing. Instead, we can set up a has_many :through association.
We have a new app with Subscriber and Magazine models. We want people to be able to subscribe to multiple magazines, and we want to be able to look up all of a magazine's subscribers.
We could do this through a
has_and_belongs_to_many association. We'd have a simple join table with
magazine_id columns that could be used to link Subscribers to Magazines. But what if we want to track additional info regarding the subscription itself, like how many months a person is subscribed to a magazine for?
has_and_belongs_to_many associations can't do that sort of thing.
Instead, we could set up a
has_many :through association.
has_many :through basically says that one model is associated with many instances of another model through a third model. In this case, we would say that a Subscriber has many Magazines through Subscriptions.
It works a lot like a
has_and_belongs_to_many association. The table for the Subscriptions model has ID fields for the two models it's joining, Subscribers and Magazines.
But in addition to the Subscriber and Magazine ID fields, it also has an ID field of its own, allowing you to look up Subscriptions independently of Subscribers or Magazines.
And because a Subscription is a model on its own, it can also have additional attributes. In
has_many :through, two models are still joined by a database table, but that table also represents an entire third model.
You can set up the app we use in this video using the following shell commands:
rails new periodical cd periodical rails g model Subscriber name:string rails g model Magazine title:string bin/rails db:migrate bin/rake db:seed
And you can generate test data using the following commands in the Rails console:
Subscriber.create(name: 'Jay') Subscriber.create(name: 'Pasan') Magazine.create(title: 'Ruby Reader') Magazine.create(title: 'iOS Inspired')
- Generate Subscription model
bin/rails g model Subscription months:integer subscriber_id:integer magazine_id:integer bin/rails db:migrate
- Need to update model classes with associations
class Subscription < ApplicationRecord belongs_to :subscriber belongs_to :magazine end
class Subscriber < ApplicationRecord has_many :subscriptions has_many :magazines, through: :subscriptions end
class Magazine < ApplicationRecord has_many :subscriptions has_many :subscribers, through: :subscriptions end
- Create a subscription to a magazine, add to subscriber:
subscription = Subscription.create(months: 12, magazine: Magazine.find_by(title: "Ruby Reader")) Subscriber.find_by(name: "Jay").subscriptions << subscription
- Subscription now associated with subscriber. Show it with:
pp Subscriber.find_by(name: "Jay").subscriptions
- Magazine also associated with subscriber, through subscription. Show it with:
pp Subscriber.find_by(name: "Jay").magazines
- Can also auto-create subscription model record by adding magazine direct to subscriber
Subscriber.find_by(name: "Jay").magazines << Magazine.find_by(title: "iOS Inspired") pp Subscriber.find_by(name: "Jay").subscriptions
- Auto-created join model won't have its attributes set so you may need to update those
pp Subscriber.find_by(name: "Jay").subscriptions.last.update(months: 6) pp Subscriber.find_by(name: "Jay").subscriptions
There's a lot more you can do with
has_many :through associations. You can learn more from the official Rails Guides.
Now we're going to be learning about another type of association that you'll
probably be using more frequently than has and belongs to many.
It's called has many three.
Here we have a new app with subscriber and magazine models.
We want people to be able to subscribe to multiple magazines and
we wanna be able to look up all of the magazine subscribers.
We could do this through a has and belongs to many association.
We'd have a simply joined table with subscriber id and
magazine id call ups that could be used to link subscribers to magazines.
But what if we wanna track additional info regarding the subscription itself,
like how many months a person has subscribed to a magazine for?
Has and belongs to many associations can't do that sort of thing.
Instead, we could set up a has many through association.
Has many through basically says that one model is associated with many instances of
another model through a third model.
In the case,
we could say that a subscriber has many magazines through subscriptions.
A subscription model instance joins a subscriber to a magazine and
a magazine to a subscriber.
It works a lot like a has and belongs to many association.
The table for the subscriptions model has id fields for
the two models it's joined, subscribers and magazines.
But in addition to the subscriber and magazine id fields,
it also has an id field of its own.
Allowing you to look up subscriptions independently of subscribers or magazines.
[SOUND] And because a subscription is a model on its own,
it can also have additional attributes.
Here we show an attribute that tracks the number of months a subscription is for.
In a has many through association, two models are still joined by a database
table but that table also represents an entire third model.
The first thing we're going to need to do is generate the subscription model.
So we'll run a bin, rails, generate model subscription.
We'll set up the subscription zone attributes, so
the number of months, that's an integer.
And then we'll need to set up the foreign keys that will associate the subscriber
and the magazine together, so subscriber, _id is gonna be an integer.
And that'll reference the id column of the subscriber's table.
And then we'll need to do the same for
magazines, magazine_id, that's also gonna be an integer.
Then we'll need to go through and update all three model classes with associations.
So we'll go to the newly generated subscription model.
And we're going to say that it belongs to, A subscriber,
And it also belongs to, A magazine.
That will allow us to access the subscriber and
the magazine if we look at the subscription first.
Then we'll need to go into the subscriber model class, And
we're going to say that a subscriber has many, Subscriptions.
That'll allow us to look up the subscriptions that belong to a subscriber.
But we're also going to say that it has many, Magazines,
Through its subscriptions.
This will add a magazines method to any subscriber model instance that will look
up all subscriptions the subscriber has.
And then look up all magazines that belong to those descriptions.
We'll also need to do the same for the magazine model class.
We'll say that magazine has many, Subscriptions,
And it has many, Subscribers,
Through its subscriptions.
Okay, let's save all that and
go to our Rails console, And try our new associations out.
First, we'll create a new subscription object.
So we'll say, subscription = Subscription class.create,
we'll set its month attribute to 12, so this will be a 12 month subscription.
And we'll say the magazine that the subscription is for
is a result of magazine, Class find_by,
And we'll look a magazine up by title or look up magazine titled Ruby Reader.
I'm getting the error, could not find table subscriptions and
I know exactly what happened, I forgot to run my migrations.
So bin/rails db migrate.
There we go, now I'll run Rails console again.
And previously, my call to subscription.create was unsuccessful
because the table didn't exist.
This time it should work.
Yeah, there we go.
So we've created a 12 month subscription to the Ruby Reader magazine,
now let's find the subscriber.
Subscriber.find_by, Could have find one with a name of Jay,
And I'm going to add to that subscriber's subscriptions collection.
I'm gonna add the current subscription to it.
And that subscription should now be associated with that subscriber.
So let's pretty print Subscriber.find_by, Name Jay.
And let's print that subscribers collection of subscriptions.
So there's that 12 month subscription.
And the magazine itself should also be associated with that subscriber
through the subscription.
So I'll pretty print the subscriber Jay's magazines,
And there's the Ruby Reader magazine.
If we don't wanna create a subscription manually,
we can auto-create it by adding a magazine direct to the subscriber.
So let's say, Subscriber find by name, Jay.
Magazines, And we'll append to that collection, magazine find by,
Title, iOS Inspired.
And now if we look at that subscriber's collection of subscriptions,
we'll see a new automatically created subscription on there.
The auto-created join model won't have its attributes set by default though.
You'll notice that this subscription is for nill months.
So you'll need to update those attributes if you want those set.
We can say, Subscriber find_by name Jay subscriptions.last.update,
and we'll update its months attribute to 6.
Now if we print the list of subscriptions again, we'll see that the last ones,
months attribute has been set to 6.
There's a lot more you can do with has many through associations.
See the teacher's notes if you'd like more info.
You need to sign up for Treehouse in order to download course files.Sign up