0
我正在運行rails遷移,其中對於所有記錄,我想根據現有列的值設置新列的結果值 - '得分了'。ActiveRecord Migrations - 根據現有列的值設置新列的值
class AddResultToReports < ActiveRecord::Migration
def change
add_column :reports, :result, :integer
Report.all.each do |r|
r.update_attribute(:result, 0) if (r.score < 40)
r.update_attribute(:result, 1) if (r.score >= 40)
end
add_index :reports, :result
end
end
但是當我輸入「斌/耙分貝:遷移」我得到以下錯誤:
rake aborted! StandardError: An error has occurred, this and all later migrations canceled: undefined method `<' for nil:NilClass
請幫幫忙,我怎麼能寫一個有條件的「如果」的軌道遷移?
是啊,如果得分是零,這是試圖比較,如果「零<40」,這就是爲什麼它是失敗的。我的錯。謝謝! – GregF