我有这个代码行 <%= f.collection_select :owner_ids, Owner.order(:id), :id, :name, {}, {multiple: true} %>
,它以大众媒体形式返回所有者列表,但我还需要在这个领域也包括公司,所以我也可以在同一个列表中包含公司,因为可以拥有大众媒体频道由个人或公司。
公司.rb
class Company < ActiveRecord::Base
has_many :ownerships
has_many :massmedia, through: :ownerships
has_many :owners, through: :ownerships
end
所有者.rb
class Owner < ActiveRecord::Base
has_many :ownerships
has_many :massmedia, through: :ownerships
has_many :companies, through: :ownerships
end
大众媒体.rb
class Massmedium < ActiveRecord::Base
belongs_to :category
has_many :ownerships
has_many :owners, through: :ownerships
has_many :companies, through: :ownerships
end