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

Massmedium.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 に答える