Hello, I''ve used has_many :through many times with success but for somereason am being stymied this time. Perhaps someone else can see what I am not. The three relevant models are these: class Playlist < ActiveRecord::Base has_many :songs, :through => ''playlist_songs'', :source=>:song has_many :playlist_songs end class PlaylistSong < ActiveRecord::Base set_table_name ''playlist_songs'' belongs_to :playlist belongs_to :song end class Song < ActiveRecord::Base has_many :playlists, :trhough=>''playlist_songs'' end when I try and use Playlist.songs i get an error, demonstrated below,>> pl=Playlist.find :first=> #<Playlist id: 1, name: "stimbletunes", updated_at: "2008-07-28 16:38:10", description: nil, created_at: "2008-07-28 16:38:10">>> pl.songsActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association "playlist_song" in model Playlist from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/ active_record/reflection.rb:195:in `check_validity!'' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/ active_record/associations/has_many_through_association.rb:5:in `initialize'' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/ active_record/associations.rb:1128:in `new'' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/ active_record/associations.rb:1128:in `songs'' from (irb):7>> pl.songs.newActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association "playlist_song" in model Playlist If anyboday has an idea what I should do differently, please let me know. Thanks, Michael Fairchild --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
mfairchi wrote:> class Playlist < ActiveRecord::Base > has_many :songs, :through => ''playlist_songs'', :source=>:song > has_many :playlist_songs > endUh... class Playlist < ActiveRecord::Base has_many :playlist_songs has_many :songs, :through => ''playlist_songs'', :source=>:song end If I were writing has_many, I would not delay its evaluation. So it could only see class attributes declared above it. Always remember the area between class and end is itself a series of statements that get evaluated in order. -- Phlip --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
What happens if you just leave the :source part out? I''ve never had to use that. Op dinsdag 29 juli 2008 20:00:23 UTC+2 schreef mfairchi het volgende:> > Hello, > I''ve used has_many :through many times with success but for > somereason am being stymied this time. Perhaps someone else can see > what I am not. > The three relevant models are these: > > class Playlist < ActiveRecord::Base > has_many :songs, :through => ''playlist_songs'', :source=>:song > has_many :playlist_songs > end > > class PlaylistSong < ActiveRecord::Base > set_table_name ''playlist_songs'' > belongs_to :playlist > belongs_to :song > end > > class Song < ActiveRecord::Base > has_many :playlists, :trhough=>''playlist_songs'' > end > > when I try and use Playlist.songs i get an error, demonstrated below, > > >> pl=Playlist.find :first > => #<Playlist id: 1, name: "stimbletunes", updated_at: "2008-07-28 > 16:38:10", description: nil, created_at: "2008-07-28 16:38:10"> > >> pl.songs > ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find > the association "playlist_song" in model Playlist > from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/ > active_record/reflection.rb:195:in `check_validity!'' > from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/ > active_record/associations/has_many_through_association.rb:5:in > `initialize'' > from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/ > active_record/associations.rb:1128:in `new'' > from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/ > active_record/associations.rb:1128:in `songs'' > from (irb):7 > >> pl.songs.new > ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find > the association "playlist_song" in model Playlist > > > If anyboday has an idea what I should do differently, please let me > know. > Thanks, > Michael Fairchild-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/UUnbaJZFDjQJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.