# Viewing object associated via "has\_many :through"?

**URL:** <https://www.ruby-forum.com/t/viewing-object-associated-via-has-many-through/84531>\
**Category:** Rails\
**Created:** [February 5, 2007, 7:05pm UTC](https://www.ruby-forum.com/t/viewing-object-associated-via-has-many-through/84531 "2007-02-05T19:05:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisbotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f14d63/32.png) [@chrisbotor](https://www.ruby-forum.com/u/chrisbotor)\
**Post date:** [February 5, 2007, 7:05pm UTC](https://www.ruby-forum.com/t/viewing-object-associated-via-has-many-through/84531/1 "2007-02-05T19:05:41Z")

</div>

This is my first question to the Forum. Iâ€™ve set up these associations:

class Playlist \< ActiveRecord::Base  
â€¦  
has\_many :playlists\_tracks, :dependent =\> :destroy  
has\_many :track\_ones, :through =\> :playlists\_tracks, :source =\> :track  
has\_many :track\_twos, :through =\> :playlists\_tracks, :source =\> :track  
end

class Track \< ActiveRecord::Base  
â€¦  
has\_many :playlists\_tracks, :dependent =\> :destroy  
has\_many :playlists, :through =\> :playlists\_tracks  
has\_many :playlists, :through =\> :playlists\_tracks  
end

class PlaylistsTrack \< ActiveRecord::Base  
belongs\_to :playlist  
belongs\_to :track\_one, :class\_name =\> :track, :foreign\_key =\>  
‘track\_one\_id’  
belongs\_to :track\_two, :class\_name =\> :track, :foreign\_key =\>  
‘track\_two\_id’  
end

With them Iâ€™m able to save to the playlists\_tracks table the following:  
id, playlist\_id, track\_one\_id, track\_two\_id. That all seems to work  
fine.

But no matter what I put in my playlists/show view file, I get an error  
message when I attempt to view the tracks associated with a playlist.  
For example:

\<% for track in @playlist.tracks %\>

\<%= playlist.track\_one.title %\>

\<% end %\>

gets me this:

undefined method `tracks’ for #Playlist:0xeb4a380

I think the problem revolves around the “class\_name” and “source”  
options, as I have no problem displaying the name of the Playlistcreator  
(who is also associated with Playlist through a “has many :through”  
association, but without the â€œclass\_nameâ€ and â€œsourceâ€ options) via the  
playlists/show view.

Where am I going wrong? What do I need to do to fix this?

---

<div class="post-metadata">

**Author:** ![chrisbotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f14d63/32.png) [@chrisbotor](https://www.ruby-forum.com/u/chrisbotor)\
**Post date:** [February 5, 2007, 8:53pm UTC](https://www.ruby-forum.com/t/viewing-object-associated-via-has-many-through/84531/2 "2007-02-05T20:53:05Z")

</div>

You want to use @playlists.playlists\_tracks in your view …

On Feb 5, 1:05 pm, Christian B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![chrisbotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f14d63/32.png) [@chrisbotor](https://www.ruby-forum.com/u/chrisbotor)\
**Post date:** [February 5, 2007, 9:26pm UTC](https://www.ruby-forum.com/t/viewing-object-associated-via-has-many-through/84531/3 "2007-02-05T21:26:52Z")

</div>

Bill S. wrote:

> You want to use @playlists.playlists\_tracks in your view …

Thank you Bill!

That has got me a step further. This in the view, for example:

\<% for track\_one in @playlist.playlists\_tracks %\>

\<%= track\_one.id %\>

\<% end %\>

now displays the track\_one.id, which is a good sign.

But I still can’t reach beyond the playlists\_tracks join table to pull  
the title of the track from the tracks table itself. I need the “title”  
of the track where the “track.id” is the same as the “track\_one.id”. I  
think I need a trick to kind of weave through the “:source” and  
“:class\_name” options to get to the tracks table, but maybe I’m missing  
something obvious here. Any ideas?

Again, many thanks for your answer.

---

<div class="post-metadata">

**Author:** ![chrisbotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f14d63/32.png) [@chrisbotor](https://www.ruby-forum.com/u/chrisbotor)\
**Post date:** [February 5, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/viewing-object-associated-via-has-many-through/84531/4 "2007-02-05T22:55:56Z")

</div>

Bill S. wrote:

> I think what you need to do is leverage your has\_many :through  
> relationship. Something like:
> 
> \<% for track in @playlist.track\_ones %\>
> 
> \<%= track.title %\> \<% end %\>

Thanks again. I’d already tried that and got this error message:

Could not find the source association(s) :track in model PlaylistsTrack.  
Try 'has\_many :track\_ones, :through =\> :playlists\_tracks, :source =\>  
'. Is it one of :track\_two, :playlist, or :track\_one?

I’ve been through all the different variants I can think of (like the  
odd one in my original example), but they all throw up an error message.  
I tried acting on the error message I just listed, but didn’t get  
anywhere with it. Can you or anyone see any wisdom in it that’ll help  
me?

> I don’t know your data model, however, the “track\_ones” thing kind of  
> smells a bit of an incorrect data model.

Bad choice of name, I agree. It just refers to which position in the  
form the track was selected at, and that’s arbitrary, non-essential  
information. I’ll have a think about that, though, as people don’t tend  
to give their kids ordinals as names, and you may be right. But even if  
my model is a bit skewed, I think I’d be having the same problem with  
“jazz\_tracks” and “rock\_tracks”, etc.

Can you or anyone else think what the problem might be? I’m pretty sure  
it relates to the “:source” and “:class\_name” options. Again, I have no  
problem saving all the relevant data to the playlists\_tracks join table,  
but then can’t access the tracks table afterward to grab the track name.

---

<div class="post-metadata">

**Author:** ![chrisbotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f14d63/32.png) [@chrisbotor](https://www.ruby-forum.com/u/chrisbotor)\
**Post date:** [February 6, 2007, 6:33pm UTC](https://www.ruby-forum.com/t/viewing-object-associated-via-has-many-through/84531/5 "2007-02-06T18:33:47Z")

</div>

I believe that Rails uses the :source to identify the foreign key in  
the join table for the :through relationship. Try this …

class Playlist \< ActiveRecord::Base  
…  
has\_many :playlists\_tracks, :dependent =\> :destroy  
has\_many :track\_ones, :through =\> :playlists\_tracks, :source  
=\> :track\_one  
has\_many :track\_twos, :through =\> :playlists\_tracks, :source  
=\> :track\_two  
end

Then use the your :through relationships in the view as before …

On Feb 5, 4:55 pm, Christian B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![chrisbotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f14d63/32.png) [@chrisbotor](https://www.ruby-forum.com/u/chrisbotor)\
**Post date:** [February 5, 2007, 10:10pm UTC](https://www.ruby-forum.com/t/viewing-object-associated-via-has-many-through/84531/6 "2007-02-05T22:10:18Z")

</div>

I think what you need to do is leverage your has\_many :through  
relationship. Something like:

\<% for track in @playlist.track\_ones %\>

 \<%= track.title %\> \<% end %\>

or more idiomatically

\<% @playlist.track\_ones.each do |track| %\>

 \<%= track.title %\> \<% end %\>

I don’t know your data model, however, the “track\_ones” thing kind of  
smells a bit of an incorrect data model. In general, if you have a  
table  
that enumerates a set of columns (track\_1, track\_2, …) it may  
indicate that you are not modeling a parent → child relationship. It  
was just a thought … I am probably barking up the wrong tree …

On Feb 5, 3:26 pm, Christian B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![chrisbotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f14d63/32.png) [@chrisbotor](https://www.ruby-forum.com/u/chrisbotor)\
**Post date:** [February 6, 2007, 7:03pm UTC](https://www.ruby-forum.com/t/viewing-object-associated-via-has-many-through/84531/7 "2007-02-06T19:03:50Z")

</div>

Yes, you’re right!

With this model (like you said):

class Playlist \< ActiveRecord::Base  
…  
has\_many :playlists\_tracks, :dependent =\> :destroy  
has\_many :track\_ones, :through =\> :playlists\_tracks, :source  
=\> :track\_one  
has\_many :track\_twos, :through =\> :playlists\_tracks, :source  
=\> :track\_two  
end

And this in the playlists/show view:

\<% for track\_one in @playlist.track\_ones %\>

\<%= track\_one.title %\>

\<% end %\>

I can display the track’s name.

Bill, Thank you so much! I really appreciate your walking me and my  
noobishness through this. I can now link the last pieces of my  
application together, and that’s a great relief. Cheers!
