Ruby on rails 迁移步骤会上下变化

Ruby on rails 迁移步骤会上下变化,ruby-on-rails,database,postgresql,ruby-on-rails-4,migration,Ruby On Rails,Database,Postgresql,Ruby On Rails 4,Migration,我的问题很简单。我创建了这个迁移文件,我的移动列没有更改,但创建了def change。是因为rails忽略了def up和def down?如果是,为什么 def change add_column :posts, :address, :string end def up execute 'ALTER TABLE posts ALTER COLUMN mobile TYPE integer USING (mobile::integer)' end def down execut

我的问题很简单。我创建了这个迁移文件,我的移动列没有更改,但创建了
def change
。是因为rails忽略了
def up
def down
?如果是,为什么

def change
  add_column :posts, :address, :string
end


def up
 execute 'ALTER TABLE posts ALTER COLUMN mobile TYPE integer USING (mobile::integer)'
end

def down
  execute 'ALTER TABLE posts ALTER COLUMN mobile TYPE text USING (mobile::text)'

end

Rails不会按设计同时运行change和up方法,因此会忽略change方法之后的所有内容。当您需要运行一些特定的逻辑时,比如在Up和Down方法中,您有两种选择。您可以将change方法中的内容放入up和down方法中,也可以将up和down内容放入change方法中。如果您想以“Rails4”的方式进行此操作,您应该使用
change
reversible
方法来获得所需的:

class SomeMigration < ActiveRecord::Migration

 def change
    add_column :posts, :address, :string

    reversible do |change|
      change.up do
       execute 'ALTER TABLE posts ALTER COLUMN mobile TYPE integer USING (mobile::integer)'
      end

      change.down do
        execute 'ALTER TABLE posts ALTER COLUMN mobile TYPE text USING (mobile::text)'
      end
    end
  end
classsomemigration