Could not find the association

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

mfairchi wrote:

class Playlist < ActiveRecord::Base   has_many :songs, :through => 'playlist_songs', :source=>:song   has_many :playlist_songs end

Uh...

  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.

Hello,   You may be right that I should have my has_many before the has_many :trhough, however my understanding (from the example in the rails api) is that the has_many :playlist_songs part is optional, and I should only need the has_many :songs, :through=>:playlist_songs.

  In any case, I tried moving things around as per your suggestion, but It did not change the error.

~Michael

What happens if you just leave the :source part out? I’ve never had to use that.