通过迁移插入新记录是一种好习惯吗?最近,我在从头开始重新运行本地迁移时遇到了一个奇怪的错误。它会抛出这样的错误(例如:产品模型,成本列):
undefined method 'cost=' for #<Product:0x10f60f4b8>
移民:
class AddNewProducts < ActiveRecord::Migration
def self.up
product1 = Product.new
product1.cost = 10
....
product1.save!
end
end
之前在迁移中添加了列成本:
Class AddCosttoProducts < ActiveRecord::Migration
def self.up
add_column :product, :cost, :integer, :default => 0, :null => false
end
def self.down
remove_column product, :cost
end
end
关于为什么会发生的任何提示?