开发者

Rails 3.1 Unable to create with has_one :polymorphic model from rails console

开发者 https://www.devze.com 2023-04-11 21:33 出处:网络
I have the following models: class MenuItem < ActiveRecord::Base has_one :price, :as => :pricable

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号