0

我有这个代码行 <%= 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
4

1 回答 1

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"] 等值

于 2012-05-19T10:35:24.187 回答