这是我的模型:
class Speaker < ActiveRecord::Base
belongs_to :session, :foreign_key => :session_id, :class_name => :Session
belongs_to :speakable, :polymorphic => true
end
class Session < ActiveRecord::Base
has_many :speakers
accepts_nested_attributes_for :speakers
end
class Person < ActiveRecord::Base
has_many :speakers, :as => :speakable
end
class Company < ActiveRecord::Base
has_many :speakers, :as => :speakable
end
我现在想做的是这样的:app/views/sessions/edit.html.erb
<% f.fields_for :speakers do |sf| %>
<p>
<%= sf.label :speaker %><br />
<%= sf.collection_select :speakable, Company.all + Person.all, :id, :full_name %>
</p>
<% end %>
但由于多态分配,它不起作用。我该如何解决这个问题?
编辑:错误是:
undefined method `base_class' for String:Class
参数为:
"speaker"=>{"speakable"=>"1063885469", "session_id"=>"1007692731"}
传递给 speakable 的值是发言人/公司的 id。是的,这是我指定collection_select
要返回的值,但我怎样才能同时提供两个值 (speakable_id
和speakable_type
) ?