これは私のモデルです:
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 に渡される値は、Speaker/Company の ID です。はい、これがcollection_select
返すように指定した値ですが、どうすれば両方の値 (speakable_id
とspeakable_type
) を指定できますか?