# HABTM, getting join model vars

**URL:** <https://www.ruby-forum.com/t/habtm-getting-join-model-vars/91297>\
**Category:** Rails\
**Created:** [April 6, 2007, 12:52pm UTC](https://www.ruby-forum.com/t/habtm-getting-join-model-vars/91297 "2007-04-06T12:52:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ab097/32.png) [@bartz](https://www.ruby-forum.com/u/bartz)\
**Post date:** [April 6, 2007, 12:52pm UTC](https://www.ruby-forum.com/t/habtm-getting-join-model-vars/91297/1 "2007-04-06T12:52:13Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![bartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ab097/32.png) [@bartz](https://www.ruby-forum.com/u/bartz)\
**Post date:** [April 6, 2007, 2:49pm UTC](https://www.ruby-forum.com/t/habtm-getting-join-model-vars/91297/2 "2007-04-06T14:49:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ab097/32.png) [@bartz](https://www.ruby-forum.com/u/bartz)\
**Post date:** [April 6, 2007, 2:59pm UTC](https://www.ruby-forum.com/t/habtm-getting-join-model-vars/91297/3 "2007-04-06T14:59:18Z")

</div>

On 4/6/07, Michael W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![bartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ab097/32.png) [@bartz](https://www.ruby-forum.com/u/bartz)\
**Post date:** [April 6, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/habtm-getting-join-model-vars/91297/4 "2007-04-06T16:30:15Z")

</div>

Jason R. wrote:

> On 4/6/07, Michael W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
