HABTM, getting join model vars

Bart Zonneveld wrote:

Hello all,

I having a lot of difficulty wrapping my head around the following. I'm
building a site for musicians, who can post Events where they perform.
So, Profile has_many :events, and Event habtm :profiles.

However, I want artists to be able to add additional people to their
show, such as people who aren't registered on my site. An example. DJ
Random is playing at this sweet club, together with his mate DJ John
Doe. They're both registered to my site, so looking up their profiles is
easy. However, DJ Tiesto (or insert any other dj here) is playing there
as well, and DJ Random wants to add Tiesto to his listing too. But,
Tiesto isn't registered to my site.

I was thinking about storing the "additional artists" (Tiesto in this
example) as a field in the join table, but I can't get those fields in
my event.artists collection.
When I query event.artists, Rails runs this SQL command: SELECT * FROM
profiles INNER JOIN artists_events ON profiles.id =
artists_events.artist_id WHERE (artists_events.event_id = 1 ), which
obviously doesn't get me the artists who don't have a profile.

Any ideas on how to achieve this? Thanks a bunch!

has_many :through may help you though I'm not sure that's necessarily the best approach.

has_many :through is now the preferred way of many-to-many relationships. Basically, you’ll want to do:

class DJ < AR::Base
has_many :dj_listings

has_many :venues, :through => :dj_listings

class Venue < AR::Base
has_many :dj_listings
has_many :djs, :through => :dj_listings

class DjListing < AR::Base
belongs_to :dj

belongs_to :venue

And DjListing will then be available to add an ‘additional_artists’ field or whatever you see fit.