Rails 3 Migration Alter Float Length / Decimal

时间:2011-06-19 09:19:14

标签: ruby-on-rails ruby-on-rails-3

如何更改Rails 3迁移文件中float列的decimalslength属性。我试过以下没有成功:

class IncreaseLatitudeLongitudeFieldLengths < ActiveRecord::Migration
  def self.up

    change_column :skateparks, :latitude, :float, {:length => 15, :decimals => 12}
    change_column :skateparks, :longitude, :float, {:length => 15, :decimals => 12}

  end

  def self.down

    change_column :skateparks, :latitude, :float, {:length => 0, :decimals => 0}
    change_column :skateparks, :longitude, :float, {:length => 0, :decimals => 0}

  end
end

1 个答案:

答案 0 :(得分:8)

个人经验最有效(因为MySQL / sqlite有时会拒绝对列的更改):创建新列,复制数据,删除旧列,重命名新列。

# Example for latitude

add_column :skateparks, :latitude2, :decimal, :precision => 15, :scale => 12
execute "UPDATE skateparks SET latitude2 = latitude"
remove_column :skateparks, :latitude
rename_column :skateparks, :latitude2, :latitude

编辑:第二次看:float, { :length => 15, :decimals => 12 }似乎是错误的。我认为你的意思是::decimal, :precision => 15, :scale => 12