Rails迁移问题

时间:2013-04-22 05:31:03

标签: ruby-on-rails

通过迁移插入新记录是一种好习惯吗?最近,从头开始重新运行本地迁移时出现了一个奇怪的错误。它抛出这样的错误(例如:产品型号,成本列):

  

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

有关为何会发生的任何提示?

1 个答案:

答案 0 :(得分:1)

如果您已经运行了上一次迁移(添加cost字段),请在添加记录之前尝试重置列信息。

class AddNewProducts < ActiveRecord::Migration
  def self.up
    Product.reset_column_information
    product1 = Product.new
    product1.cost = 10
    ....
    product1.save!
  end
end
相关问题