Ruby on Rails Thursday, March 1, 2012

On Fri, Mar 2, 2012 at 6:57 AM, Soichi Ishida <lists@ruby-forum.com> 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

> 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.

Yes, with decimal:

"... 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. ..."

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

HTH,

Peter

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment