2012-05-19 44 views
0

我在形式代碼行<%= f.collection_select :owner_ids, Owner.order(:id), :id, :name, {}, {multiple: true} %>返回大衆媒體形式的業主名單,但我需要在這個領域也包括公司,所以我可以在同樣的公司名單,導致一個大衆傳媒chanel可能由一個人或一家公司擁有。f.collection_select從軌道2中的2表3

Company.rb

class Company < ActiveRecord::Base 

    has_many :ownerships 
    has_many :massmedia, through: :ownerships 
    has_many :owners, through: :ownerships 
end 

Owner.rb

class Owner < ActiveRecord::Base 

    has_many :ownerships 
    has_many :massmedia, through: :ownerships 
    has_many :companies, through: :ownerships 
end 

Massmedium.rb

class Massmedium < ActiveRecord::Base 
    belongs_to :category 

    has_many :ownerships 
    has_many :owners, through: :ownerships 
    has_many :companies, through: :ownerships 

end 

回答

0
= f.select :owners, options_for_select((Owner.all + Company.all) 
    .collect{|o| [o.name, "#{o.class},#{o.id}"], {multiple: true} 

您必須在您的模型中定義owners=(values)方法,該方法將解析像[「Company,1」,「Owner,2」]的值