# Has\_many through

**URL:** <https://www.ruby-forum.com/t/has-many-through/72134>\
**Category:** Rails\
**Created:** [October 14, 2006, 4:42pm UTC](https://www.ruby-forum.com/t/has-many-through/72134 "2006-10-14T16:42:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicholas\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f17d59/32.png) [@Nicholas\_W](https://www.ruby-forum.com/u/Nicholas_W)\
**Post date:** [October 14, 2006, 4:42pm UTC](https://www.ruby-forum.com/t/has-many-through/72134/1 "2006-10-14T16:42:53Z")

</div>

Hi \*,  
I have an issue with a relation I’m not able to understand: I have 3  
tables, a releases table, a tracks table and a release\_tracks table.  
The release\_tracks table has a release\_id and a track\_id.  
Release and Track models have both a has\_many :through  
=\> :release\_tracks association (has\_many :releases and  
has\_many :tracks).  
I’m just not able to make it work …  
For example

> > r = Release.new  
> > =\> #\<Release:0x3732258 @attributes={“title”=\>nil, “inserted\_at”=\>nil,  
> > “mb\_id”=\>nil}, @new\_record=true\>  
> > r.tracks  
> > ActiveRecord::HasManyThroughAssociationNotFoundError:  
> > ActiveRecord::HasManyThroughAssociationNotFoundError  
> > from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/  
> > lib/active\_record/reflection.rb:169:in `check_validity!' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/ lib/active_record/associations/has_many_through_association.rb:6:in `initialize’  
> > from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/  
> > lib/active\_record/associations.rb:876:in `new' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/ lib/active_record/associations.rb:876:in `tracks’  
> > from (irb):48

> > t = Track.new  
> > =\> #\<Track:0x372f918 @attributes={“artist\_id”=\>nil, “title”=\>nil,  
> > “inserted\_at”=\>nil, “mb\_id”=\>nil, “position”=\>nil, “duration”=\>nil},  
> > @new\_record=true\>  
> > r.tracks \<\< t  
> > ActiveRecord::HasManyThroughAssociationNotFoundError:  
> > ActiveRecord::HasManyThroughAssociationNotFoundError  
> > from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/  
> > lib/active\_record/reflection.rb:169:in `check_validity!' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/ lib/active_record/associations/has_many_through_association.rb:6:in `initialize’  
> > from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/  
> > lib/active\_record/associations.rb:876:in `new' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/ lib/active_record/associations.rb:876:in `tracks’  
> > from (irb):50

class Release \< ActiveRecord::Base

```
has_many :tracks, :through => :release_tracks

```

end

class Tracks \< ActiveRecord::Base

```
has_many :releases, :through => :release_tracks

```

end

class ReleaseTracks \< ActiveRecord::Base

```
belongs_to :release
belongs_to :track

```

end

What’s wrong here ? Is the ReleaseTracks model really needed ?

TIA,  
ngw  
Chiacchiera con i tuoi amici in tempo reale!  
[Yahoo Search - Ricerca nel Web | Motore di Ricerca](http://it.yahoo.com/mail_it/foot/*http://it.messenger.yahoo.com)

---

<div class="post-metadata">

**Author:** ![Nicholas\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f17d59/32.png) [@Nicholas\_W](https://www.ruby-forum.com/u/Nicholas_W)\
**Post date:** [October 14, 2006, 9:56pm UTC](https://www.ruby-forum.com/t/has-many-through/72134/2 "2006-10-14T21:56:56Z")

</div>

Nicholas W. wrote:

> Hi \*,  
> I have an issue with a relation I’m not able to understand: I have 3  
> tables, a releases table, a tracks table and a release\_tracks table.  
> The release\_tracks table has a release\_id and a track\_id.  
> Release and Track models have both a has\_many :through  
> =\> :release\_tracks association (has\_many :releases and  
> has\_many :tracks).  
> I’m just not able to make it work …
> 
> class Release \< ActiveRecord::Base  
> has\_many :tracks, :through =\> :release\_tracks  
> end
> 
> class Tracks \< ActiveRecord::Base  
> has\_many :releases, :through =\> :release\_tracks  
> end
> 
> class ReleaseTracks \< ActiveRecord::Base  
> belongs\_to :release  
> belongs\_to :track  
> end
> 
> What’s wrong here ? Is the ReleaseTracks model really needed ?

You are missing the association to the join model. Release should look  
like this:

class Release \< ActiveRecord::Base  
has\_many :release\_tracks  
has\_many :tracks, :through =\> :release\_tracks  
end

And likewise for Tracks. I have many examples of hmt use on my blog.  
[http://blog.hasmanythrough.com/articles/category/associations](http://blog.hasmanythrough.com/articles/category/associations)

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![Nicholas\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f17d59/32.png) [@Nicholas\_W](https://www.ruby-forum.com/u/Nicholas_W)\
**Post date:** [October 15, 2006, 2:00pm UTC](https://www.ruby-forum.com/t/has-many-through/72134/3 "2006-10-15T14:00:05Z")

</div>

Il giorno 14/ott/06, alle ore 21:56, Josh S. ha scritto:

> You are missing the association to the join model. Release should look  
> like this:
> 
> class Release \< ActiveRecord::Base  
> has\_many :release\_tracks  
> has\_many :tracks, :through =\> :release\_tracks  
> end
> 
> And likewise for Tracks. I have many examples of hmt use on my blog.  
> [http://blog.hasmanythrough.com/articles/category/associations](http://blog.hasmanythrough.com/articles/category/associations)

Thank you very much !  
Another question, because I don’t think I really got it …  
The ReleaseTrack model has an attribute, position, and I want to use  
it to store the position of the track inside the Release.  
How can I build something like this ? Like having

t = Track.new(:position =\> 1, blah blah)

or even better

release \<\< track(:position =\> 1)

because a release can have the same track in a different position.  
Is it possible ?

TIA,  
ngw

Chiacchiera con i tuoi amici in tempo reale!  
[Yahoo Search - Ricerca nel Web | Motore di Ricerca](http://it.yahoo.com/mail_it/foot/*http://it.messenger.yahoo.com)
