HABTM, getting join model vars

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!

Bart Z. wrote:

easy. However, DJ Tiesto (or insert any other dj here) is playing there

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.


Michael W.

On 4/6/07, Michael W. [email protected] wrote:

show, such as people who aren’t registered on my site. An example. DJ
profiles INNER JOIN artists_events ON profiles.id =
Michael W.
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
end

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

class DjListing < AR::Base
belongs_to :dj
belongs_to :venue
end

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

Jason

Jason R. wrote:

On 4/6/07, Michael W. [email protected] wrote:

show, such as people who aren’t registered on my site. An example. DJ
profiles INNER JOIN artists_events ON profiles.id =
Michael W.
has_many :through is now the preferred way of many-to-many
relationships.

Thanks, it works!
For others information:

class Profile
has_many :show_listings
has_many :events, :through => :show_listings
end

class Event
has_many :show_listings
has_many :profiles, :through => :show_listings
end

class ShowListing < ActiveRecord::Base
belongs_to :profile
belongs_to :event
end

Event.find(id).show_listings # gives join models, with additional params
set in the join table