# One lookup table for two different joins

**URL:** <https://www.ruby-forum.com/t/one-lookup-table-for-two-different-joins/184086>\
**Category:** Rails\
**Created:** [February 24, 2010, 9:36pm UTC](https://www.ruby-forum.com/t/one-lookup-table-for-two-different-joins/184086 "2010-02-24T21:36:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![face7hill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e480ec/32.png) [@face7hill](https://www.ruby-forum.com/u/face7hill)\
**Post date:** [February 24, 2010, 9:36pm UTC](https://www.ruby-forum.com/t/one-lookup-table-for-two-different-joins/184086/1 "2010-02-24T21:36:31Z")

</div>

Hello Folks,

I’m trying to set up a Band model that uses one lookup table  
(Instruments) to describe two things:

1. Instruments that the Band currently plays
2. Instruments that the Band needs

I only want to have one Instruments table to describe all instruments  
for maintainability. Is there a way to do this? Here is my current  
Band model:

class Band \< ActiveRecord::Base  
attr\_accessible :name, :description, :genre\_ids, :instrument\_ids

# Band Instruments played

has\_many :band\_plays\_instruments  
has\_many :instruments, :through =\> :band\_plays\_instruments

# Band Instruments needed

has\_many :band\_instrument\_needs  
has\_many :instruments, :through =\> :band\_instrument\_needs

end

The problem is that apparently I can’t have two  
“has\_many :instruments, :through =\> x” statements in the model. Does  
anybody have any recommendations as to how I should handle this?

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![face7hill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e480ec/32.png) [@face7hill](https://www.ruby-forum.com/u/face7hill)\
**Post date:** [February 25, 2010, 9:01am UTC](https://www.ruby-forum.com/t/one-lookup-table-for-two-different-joins/184086/2 "2010-02-25T09:01:15Z")

</div>

On 24 February 2010 20:35, face7hill [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Band model:  
> One table for instruments is the right idea, but you would probably be  
> better off then having separate join-tables for “instruments\_wanted”  
> and “instuments\_played”. This would give you the functionality of  
> having extra attributes dedicated to those collections: an owned  
> instrument might have a record of which band members play it; desired  
> instruments might have a price that needs to be paid to buy it.

class Band \< ActiveRecord::Base  
has\_many :instruments\_played  
has\_many :instruments\_needed  
end

class InstrumentsPlayed \< ActiveRecord::Base  
belongs\_to :instrument  
end

class InstrumentsNeeded \< ActiveRecord::Base  
belongs\_to :instrument  
end

(of course, you might want to fiddle with the :class\_name and table  
names as these aren’t intuitively pluralized - or just go with  
“instruments\_neededs” 🙂

in your code you can now use:  
@band.instruments\_played.each do |instrument\_played|  
instrument\_played.instrument …

and  
@band.instruments\_played \<\< InstrumentPlayed.create(:instrument =\>  
Instrument.find\_by\_name(“bassoon”))

Or you can add shortcuts to just the instruments (should you need) to  
the band model:  
def band\_plays\_instruments  
instruments\_played.inject([]) { |instrument\_played| instruments \<\<  
instrument\_played.instrument }  
end
