2016-07-19 8 views
5
Rails 3.2 
MySQL gem 

Ich habe folgendes in meiner Migration:Migration mit einer Dezimalzahl mit zwei hinteren Ziffern

t.decimal :pre_tax_total, default: nil, scale: 2 
t.decimal :post_tax_total, default: nil, scale: 2 

Nach dem, was ich lese, Skala: 2 wird eine Dezimalzahl mit zwei hinteren Ziffern erzeugen.

Wenn ich die Migration laufen, und an der Tabellenstruktur aussehen, sehe ich folgendes:

pre_tax_total decimal(10,0) 
post_tax_total decimal(10,0) 

Was bedeutet, die Werte vom MySQL-Server abgeschnitten bekommen. Wie lautet die ActiveRecord-Syntax zum Erstellen dieser Spalten als Dezimalzahl (10,2)?

Antwort

9

Das wäre:

t.decimal :pre_tax_total, precision: 10, scale: 2 

Obwohl Rails 3 Migrations Guide überspringt es ist die Beschreibung in der source code verfügbar:

# Note: The precision is the total number of significant digits, 
    # and 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. 
    # 
    # Please be aware of different RDBMS implementations behavior with 
    # <tt>:decimal</tt> columns: 
    # * The SQL standard says the default scale should be 0, <tt>:scale</tt> <= 
    # <tt>:precision</tt>, and makes no comments about the requirements of 
    # <tt>:precision</tt>. 
    # * MySQL: <tt>:precision</tt> [1..63], <tt>:scale</tt> [0..30]. 
    # Default is (10,0). 

Die letzte Zeile teilweise erklärt, warum Sie decimal(10,0) bekam.

+0

Es hat funktioniert. Vielen Dank. Ich könnte schwören, dass der Arzt sagte, dass Sie den Präzisionsparameter überspringen könnten – EastsideDeveloper