14

I need to change t.integer :mark_up to a float How can I do this? I have tried in my terminal rails g migration change_column(:stakes, :mark_up, :float) by keep getting a syntax error near unexpected token ('

Maxim
  • 9,701
  • 5
  • 60
  • 108
Neil Murphy
  • 267
  • 1
  • 2
  • 10

2 Answers2

27

In your terminal:

rails generate migration ChangeMarkUpToFloat

and in the file that is created: db/migrate/2015xxxxxxxxxx/change_mark_up_to_float.rb

edit it to:

class ChangeMarkUpToFloat < ActiveRecord::Migration
  def change
    change_column :stakes, :mark_up, :float
  end
end

and then back in your terminal:

rake db:migrate
Nosajool
  • 456
  • 4
  • 10
  • This is ok when the app is not in production. When the app is in production, I would recommend following engineyard's guide on [zero downtime deployment](https://blog.engineyard.com/2011/zero-downtime-deploys-with-migrations) – bsvin33t Mar 23 '15 at 17:49
  • 2
    Be careful with using the `change` method when a migration really isn't reversible. If you ran `rake db:migrate:down`, how would it know what type to convert to? Consider defining `up` and `down` instead. – maaachine Feb 16 '16 at 14:31
0

You can't use rails code (change_column) in your terminal.

What you need to do is first create the migration: rails generate migration ChangeMarkUpType and then put your rails code in the file that was created.

You can read more about migrations here

sebkkom
  • 1,426
  • 17
  • 31