Rails migration generator - how to specify decimal precision and scale


This is a simple thing to forget and this is why it deserves its own post!

The standard way to do it is:

rails g migration add_amount_to_records amount:decimal{5,2}

But this does not work if you are using zsh and instead we get the following migration:

class AddAmountToRecords < ActiveRecord::Migration[5.2]
  def change
    add_column :records, :amount, :decimal5
    add_column :records, :amount, :decimal2
  end
end

We obviously didn’t want this. In order to get what we wanted, we can replace the comma with a hyphen:

rails g migration add_amount_to_records amount:decimal{5-2}

Or simply quote the column declaration:

rails g migration add_amount_to_records 'amount:decimal{5,2}'

Both commands result in the following migration:

class AddAmountToRecords < ActiveRecord::Migration[5.2]
  def change
    add_column :records, :amount, :decimal, precision: 5, scale: 2
  end
end

Bingo!

Related Posts

The Safe Navigation Operator (&.) in Ruby

Ruby 2.3 introduces the &. operator which will make dealing with nils easier

Benchmarking Ruby

How to benchmark ruby code and compare different solutions to a problem

The Null Object pattern

A technique for dealing with certain nil errors

Hello World

Every techie's first post