3

When generating migrations via the command line you can specify the type of field you want to create. In the case of decimals this would give

rails generate migration AddAmountToOrder amount:decimal

This way you get a migration with a decimal column for your database. But you have to manually go to the file and add values for precision and scale like this

t.decimal :amount  # generated, but...
t.decimal :amount, precision: 10, scale: 2  # ... wanted!

How can you supply these values via the command line so that you can immediately do a rake db:migrate without first adding precision and scale to the migration file?

Magnus
  • 58
  • 1
  • 8

1 Answers1

14

You can specify precision and scale in your migration generator command as follows:

rails generate migration AddAmountToOrder amount:decimal{10.2}
#precision: 10, scale: 2
dp7
  • 6,651
  • 1
  • 18
  • 37