私はここで立ち往生しており、オンラインのどこにも答えがないようです. 演習では次のように述べています。
製品価格を品目にコピーする移行を作成し、カート モデルの add_product メソッドを変更して、新しい品目が作成されるたびに価格を取得します。
私のコード:
class AddPriceToLineItem < ActiveRecord::Migration
def self.up
add_column :line_items, :price, :decimal
say_with_time "Updating prices..." do
LineItem.find(:all).each do |li|
li.update_attribute :price, li.product.price
end
end
end
def self.down
remove_column :line_items, :price
end
end
私も試しました:
class AddPriceToLineItem < ActiveRecord::Migration
def self.up
add_column :line_items, :price, :decimal
LineItem.all.each do |li|
li.price = li.product.price
end
end
def self.down
remove_column :line_items, :price
end
end
このエラーが発生し続けます:
rake db:migrate
== AddPriceToLineItem: migrating =============================================
-- add_column(:line_items, :price, :decimal)
-> 0.0010s
-- Updating prices...
rake aborted!
An error has occurred, this and all later migrations canceled:
undefined method `price' for nil:NilClass
price は前の行で定義されたばかりなので、 undefined nil:nilClass と言っているのは奇妙です。
レール(3.2.1)、ルビー1.9.3p125を使用しています。
誰でも助けることができますか?