Change DB data type and limited decimals

Rails3.1.3

I have db type,

startp:integer

I need to change it to float. More specifically,

1.2, 45.1, 143.8 …

I have two questions:

  1. Is removing and adding the field through migration the only way?
    in other words, is there a single command to change the type?

  2. Is there anyway to limit the decimal place to only one?
    56.3, 34.2… are acceptable, but 23.112, 77.34, … are not.

Thanks in advance.

soichi

On Fri, Mar 2, 2012 at 6:57 AM, Soichi I. [email protected]
wrote:

Rails3.1.3

I have db type,

startp:integer

I need to change it to float. More specifically,

1.2, 45.1, 143.8 …

Don’t use float here, use decimal (if your real intention is
decimals, for counting “business” things like money,
quantity, size in centimeter, etc.).

I have two questions:

  1. Is removing and adding the field through migration the only way?
    in other words, is there a single command to change the type?

Probably with “change_column” (I am not 100% sure). Refs:
http://guides.rubyonrails.org/migrations.html
http://api.rubyonrails.org/classes/ActiveRecord/Migration.html
http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html#method-i-column

  1. Is there anyway to limit the decimal place to only one?
    56.3, 34.2… are acceptable, but 23.112, 77.34, … are not.

Yes, with decimal:

“… For claritys 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. …”

t.decimal :number, :precision => 10, :scale => 1

HTH,

Peter

thanks for your answer.
I have successfully changed the column! And decimals work perfectly for
me.

soichi

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs