与这个问题非常相似,但并不完全相同。
我有音乐家和乐器模型:
# musician.rb
class Musician < ActiveRecord::Base
attr_accessible :instrument_attributes
has_many :instrument_choices
has_many :instruments, through: instrument_choices
accepts_nested_attributes_for :instruments # with some other stuff
end
# instrument.rb
class Instrument < ActiveRecord::Base
has_many :inverse_instrument_choices, class_name: "InstrumentChoice"
has_many :musicians, through: :inverse_instrument_choices
validates_uniqueness_of :name
# instrument_choice.rb
class InstrumentChoice < ActiveRecord::Base
belongs_to :musician
belongs_to :instrument
validates_uniqueness_of :musician_id, scope: :instrument_id
我有一个可能的工具的静态列表,用户在新视图和编辑视图中的选择表单中从该列表中进行选择。假设所有这些工具都有现有记录。我如何处理在音乐家和乐器之间添加新的关联?
谢谢!