Rails 3.1通过控制台将项添加到has_one多态

时间:2011-10-11 01:45:13

标签: ruby-on-rails ruby-on-rails-3

这是对此问题的跟进:Rails 3.1 Unable to create with has_one :polymorphic model from rails console

我想知道如何添加语法(如果可能):

mi = MenuItem.new
mi.name = "Julie's Mac & Cheese"
mi.price << Price.new
mi.save

我收到以下错误:

ruby-1.9.2-p290 :009 > mi.price << Price.new
Price Load (0.2ms)  SELECT "prices".* FROM "prices" WHERE "prices"."pricable_id" = 6 AND "prices"."pricable_type" = 'MenuItem' LIMIT 1
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.

mi.price_attributes也无效。

感谢。

1 个答案:

答案 0 :(得分:0)

以上砖块与has_one相关:

mi.price = Price.new