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?
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