0

I am working through a rails tutorial, and came across this line rails g model product name decimal:{7, 2}.

What do those curly braces at the end mean? What do they do?

Originally, I thought they force a level of precision with floating point numbers, but that proved to be false. I could make a decimal 10 digits long with a decimal going to the thousandths place.

sawa
  • 165,429
  • 45
  • 277
  • 381
benbot
  • 1,217
  • 1
  • 12
  • 28
  • 3
    Those sets precision of decimals. See: http://stackoverflow.com/questions/9560233/how-to-generate-scaffold-for-data-type-with-extra-description-in-rails-3 – BroiSatse Sep 07 '13 at 15:05

2 Answers2

1

Please see for example: - http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html

There it says:

For clarity’s sake: the precision is the number of significant digits, while the scale is the number of digits that can be stored following the decimal point. For example, the number 123.45 has a precision of 5 and a scale of 2. A decimal with a precision of 5 and a scale of 2 can range from -999.99 to 999.99.

Ben
  • 301
  • 1
  • 10
1

It's the decimal field's precision (total number of digits) and scale (digits after the decimal point).

From rails g model -h:

For decimal two integers separated by a comma in curly braces will be used
for precision and scale:

    `rails generate model product price:decimal{10,2}`

From MySQL docs:

The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments in MySQL 5.1 are as follows:

  • M is the maximum number of digits (the precision). It has a range of 1 to 65. (Older versions of MySQL permitted a range of 1 to 254.)

  • D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.

Stefan
  • 109,145
  • 14
  • 143
  • 218