Rails 3 Migration Alter Float Length / Decimal

How can I change the decimals and length attributes of a float column in my Rails 3 migration file. I tried the following success:

 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 
+4
source share
1 answer

Personal experience that works best (since MySQL / sqlite sometimes discards changes to columns): Create a new column, copy data, delete the old column, rename the new column.

 # 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 

EDIT: In a second glance :float, { :length => 15, :decimals => 12 } seems wrong. I assume you meant:: :decimal, :precision => 15, :scale => 12 ?

+8
source

All Articles