Has_many through

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

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


Josh S.
http://blog.hasmanythrough.com

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

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