Rails迁移不适用

时间:2015-06-03 10:44:27

标签: ruby-on-rails ruby migration

我有以下迁移:

    class MoveInventoryItems < ActiveRecord::Migration
      def up
        schraenke = ('F'..'J')
        List::Inventory::Item.find_each do |item|
            if schraenke.include?(item.location[0])
                item.location[0] = item.location[0].next.next
                item.save!
            end
        end
      end
   end

我正在尝试更改特定项目的变量。 当我尝试在保存后输出项目时,它看起来是正确的。运行迁移时,它不会显示任何错误。但显然它并没有真正得到更新,因为当我运行服务器时仍会显示旧值。

所以我的问题是:迁移如何不更新数据?

1 个答案:

答案 0 :(得分:0)

实际上,up是类级方法,你可以用以下代码替换上面的代码,它会更新记录。

class MoveInventoryItems < ActiveRecord::Migration
  def self.up
    schraenke = ('F'..'J')
    List::Inventory::Item.find_each do |item|
      if schraenke.include?(item.location[0])
        item.location[0] = item.location[0].next.next
        item.save!
      end
    end
  end

  def self.down
     #put reverse code of above, it will execute when you rollback migration
  end
end