23

From Ruby on Rails: best method of handling currency / money, how do you generate a scaffold for the folowing:

add_column :items, :price, :decimal, :precision => 8, :scale => 2

Such as:

rails generate scaffold LineItem name:string \
                                 price:decimal {:precision => 8, :scale => 2}

Also, what is the correct term for "extra description" for the decimal type?

Working in Rails 3.07, Ruby 1.92

Community
  • 1
  • 1
B Seven
  • 44,484
  • 66
  • 240
  • 385
  • 1
    Please expand on "what is the correct term for 'extra description' for the decimal type". I simply don't understand what you are asking there. Please expand with more detail, description, example, etc. – Michael Durrant Mar 05 '12 at 00:32
  • @Michael: 'extra description' means `{:precision => 8, :scale => 2}` – B Seven Mar 05 '12 at 00:46
  • as smile-on pointed the Correct scaffold syntax **field_name:decimal{p.s}** – frenesim Jan 19 '16 at 12:14

6 Answers6

56

In Rails 3.1 and below, the syntax is

rails generate scaffold LineItem name:string price:decimal

and then manually add the decimal properties to the migration file

t.decimal :price, :precision => 8, :scale => 2

In Rails 3.2, one can specify the decimal properties

rails generate scaffold LineItem name price:decimal{8,2}

NOTE: If you are running ZSH, the syntax requires a hyphen instead of a comma.

rails generate scaffold LineItem name price:decimal{8-2}

ANOTHER NOTE: If you are using bash under Mac OS X 10.9 try a dot instead of the comma

rails generate scaffold LineItem name price:decimal{8.2}
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
scarver2
  • 7,887
  • 2
  • 53
  • 61
  • Do you know where this is documented? – Dogweather Jun 18 '12 at 05:57
  • 1
    @Dogweather The documenation is in the Rails 3.2 release notes under the Railties section http://guides.rubyonrails.org/3_2_release_notes.html#railties (all three lines of it) As for the Z-shell fix, it was embedded in this issue https://gist.github.com/rails/rails/issues/4602 – scarver2 Jul 06 '12 at 12:49
  • 1
    The `rails generate scaffold LineItem name price:decimal{8,2}` syntax didn't work for me, and I'm using Rails 3.2.3. – mjnissim Jul 29 '12 at 16:41
  • @mjnissim Is your terminal running the default bash or do you have Z-Shell installed? – scarver2 Jul 31 '12 at 14:45
  • 2
    The suggested ZSH workaround doesn't work for me, I have to quote the parameter, i.e. `rails generate scaffold LineItem name "price:decimal{8,2}"` – Stefan Sep 07 '13 at 16:16
  • @Stefan Thx for sharing. What OS and flavor of ZSH are you running? I just tested my ZSH answer again using OSX Mountain Lion, Rails 4, and Oh My ZSH and it worked correctly. – scarver2 Sep 07 '13 at 17:42
  • @scarver2 ZSH 5.0.2 with [prezto](https://github.com/sorin-ionescu/prezto) on OS X 10.8.4. It expands `foo{1-3}` to `foo1 foo2 foo3` – Stefan Sep 08 '13 at 06:07
  • This is due to to the [`BRACE_CCL`](http://zsh.sourceforge.net/Doc/Release/Expansion.html#Brace-Expansion) option. – Stefan Sep 08 '13 at 06:14
  • On OS X Lion using bash I had to use the suggested ZSH syntax. – jcm Oct 02 '13 at 10:57
  • Just in case anyone comes here while working on Mavericks with Rails 4, both `decimal{X.Y}` and `decimal{X-Y}` will do the work. Thanks @scarver2 and @BooVeMan! – sebkkom May 05 '14 at 09:36
12

A few years later, with Rails 4.2 and bash (Linux) the following generator command works without problems:

bin/rails generate scaffold LineItem name:string price:decimal{8.2}

This will correctly generate the following example migration:

class CreateLineItems < ActiveRecord::Migration
  def change
    create_table :line_items do |t|
      t.string :name
      t.decimal :price, precision: 8, scale: 2

      t.timestamps null: false
    end
  end
end
rkallensee
  • 1,994
  • 15
  • 12
11

Almost a year later. Rails 3.2.11. Regular bash shell. Rails scaffold creates mess with syntax field_name:decimal{p,s} regardless of railties official doc. The confusion lays in simple fact that curly braces are meta-characters in bash (as well as in other shells) and needs to be escaped. See logged issue 4602 in scaffold generator repo.

If you using bash then use dot instead of comma as workaround.
Correct scaffold syntax field_name:decimal{p.s}

smile-on
  • 2,073
  • 1
  • 20
  • 20
  • 2
    +1 decimal{18,2} didn't work for me in Rails4.0 and bash, this was the correct answer. – nurettin Jul 14 '13 at 17:57
  • same in rails 4, comma didn't work. haven't tried dot, just added the scale / precision manually in the migration... felt more safe to me – botbot Dec 02 '14 at 22:13
3

This is the accurate and most simple way to do this under Rails 5.x:

rails generate scaffold LineItem name price:decimal{'8,2'}

Pay special attention to the single quotes used when specifying scale and precision.

W.M.
  • 589
  • 1
  • 6
  • 13
3

New approach:

Create the migration with just add_column, e.g. rails generate migration AddPriceToLineItem price:integer

Then edit the migration and change it to be how you want it, e.g.

add_column :line_items, :price, :decimal, :precision => 8, :scale => 2

Getting the command line exactly right to do this has proved to be a major exercise in frustration and wasted time for me in the past. I recommend you follow this procedure and move on.

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
2

Here's how I did it:

rails generate scaffold LineItem ... amount:decimal ...

The ... are any other fields we need in the scaffold, like date:date, item:string, category:references...

Then I modified the migration to look like:

create_table :line_items do |t|
  .
  .
  .
  t.decimal :amount, :precision => 8, :scale => 2

Then

rake db:migrate

This holds values between -999,999.99 to 999,999.99.

Here is some (marginally) useful reference: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html

B Seven
  • 44,484
  • 66
  • 240
  • 385