Rails 3.1 Unable to create with has_one :polymorphic model from rails console
I have the following models:
class MenuItem < ActiveRecord::Base
has_one :price, :as => :pricable
accepts_nested_attributes_for :price
attr_accessi开发者_高级运维ble :price_attributes, :price
end
class Price < ActiveRecord::Base
belongs_to :pricable, :polymorphic => true
attr_accessible :price, :price_comment
end
I'm trying to insert at rails console with:
MenuItem.create({"name"=>"Julie's Mac & Cheese","price"=>{"price"=>14}})
but am getting this error:
ActiveRecord::AssociationTypeMismatch: Price(#70145189558680) expected, got Hash(#70145158648700)
How do I force this into thinking it is a Price? tried using it as symbol
thx?
MenuItem.create(:name => "Julie's Mac & Cheese", :price_attributes => {:price => 14})
edit Sorry for lack of explanation but the answer is pretty simple... you just weren't passing the right parameter. I also cleaned it up a bit.
精彩评论