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! http://it.yahoo.com/mail_it/foot/*http://it.messenger.yahoo.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Nicholas Wieland 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 Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Il giorno 14/ott/06, alle ore 21:56, Josh Susser 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/associationsThank 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! http://it.yahoo.com/mail_it/foot/*http://it.messenger.yahoo.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---