2011-10-11 35 views
0

我有以下型號:的Rails 3.1無法與HAS_ONE創造:從鐵軌控制檯多態模型

class MenuItem < ActiveRecord::Base 
    has_one :price, :as => :pricable 
    accepts_nested_attributes_for :price 
    attr_accessible :price_attributes, :price 
end 

class Price < ActiveRecord::Base 
    belongs_to :pricable, :polymorphic => true 
    attr_accessible :price, :price_comment 
end 

我想在插入軌控制檯:

MenuItem.create({"name"=>"Julie's Mac & Cheese","price"=>{"price"=>14}}) 

但我得到這個錯誤:

ActiveRecord::AssociationTypeMismatch: Price(#70145189558680) expected, got Hash(#70145158648700) 

我該如何強制它認爲這是一個價格?試圖用它作爲符號

thx?

回答

2
MenuItem.create(:name => "Julie's Mac & Cheese", :price_attributes => {:price => 14}) 

編輯對不起,缺乏解釋,但答案很簡單...你只是沒有經過正確的參數。我也清理了一下。

+0

thx - 作品很棒 – timpone