This is my first question to the Forum. I’ve set up these associations:
class Playlist < ActiveRecord::Base
…
has_many :playlists_tracks, :dependent => :destroy
has_many :track_ones, :through => :playlists_tracks, :source => :track
has_many :track_twos, :through => :playlists_tracks, :source => :track
end
class Track < ActiveRecord::Base
…
has_many :playlists_tracks, :dependent => :destroy
has_many :playlists, :through => :playlists_tracks
has_many :playlists, :through => :playlists_tracks
end
class PlaylistsTrack < ActiveRecord::Base
belongs_to :playlist
belongs_to :track_one, :class_name => :track, :foreign_key =>
‘track_one_id’
belongs_to :track_two, :class_name => :track, :foreign_key =>
‘track_two_id’
end
With them I’m able to save to the playlists_tracks table the following:
id, playlist_id, track_one_id, track_two_id. That all seems to work
fine.
But no matter what I put in my playlists/show view file, I get an error
message when I attempt to view the tracks associated with a playlist.
For example:
<% for track in @playlist.tracks %>
<% end %>
gets me this:
undefined method `tracks’ for #Playlist:0xeb4a380
I think the problem revolves around the “class_name” and “source”
options, as I have no problem displaying the name of the Playlistcreator
(who is also associated with Playlist through a “has many :through”
association, but without the “class_name†and “source†options) via the
playlists/show view.
Where am I going wrong? What do I need to do to fix this?