# Integer column, decimal in the form partial

**URL:** <https://www.ruby-forum.com/t/integer-column-decimal-in-the-form-partial/119972>\
**Category:** Rails\
**Created:** [November 12, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/integer-column-decimal-in-the-form-partial/119972 "2007-11-12T16:45:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karine\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e99b99/32.png) [@Karine\_D](https://www.ruby-forum.com/u/Karine_D)\
**Post date:** [November 12, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/integer-column-decimal-in-the-form-partial/119972/1 "2007-11-12T16:45:08Z")

</div>

Hello,

I used a migration to add an integer column to one of my data tables:  
add\_column :lien, :cpc, :integer, :null =\> false

The user is not interested in this integer, but in 1/1 000 000 of it.  
So, in my form partial I used a text\_field as usual:  
\<%= text\_field ‘lien’, ‘cpc’ %\>  
But in my controller, I modify the cpc value to get an integer:  
def edit\_lien  
@lien = Lien.find(params[:id])  
@lien.cpc /= 1000000  
end  
def update\_lien  
@lien = Lien.find(params[:id])  
@lien.update\_attributes(params[:lien])  
@lien.cpc \*= 1000000  
@lien.save  
end

This works great as long as the user enters an integer value in the  
text field (if ‘3’ is entered, the database contains 3000000 and the  
user still sees ‘3’ when re-editing). But decimal values are truncated  
(if ‘1.5’ is entered, the database contains 1000000 instead of 1500000).

I guess that’s because I defined my cpc column as integer in the first  
place, can I tell Rails to not truncate decimal values as I will use  
them, or should I have gone another way to make this work properly?

Thanks,  
Karine

---

<div class="post-metadata">

**Author:** ![Karine\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e99b99/32.png) [@Karine\_D](https://www.ruby-forum.com/u/Karine_D)\
**Post date:** [November 13, 2007, 11:07am UTC](https://www.ruby-forum.com/t/integer-column-decimal-in-the-form-partial/119972/2 "2007-11-13T11:07:05Z")

</div>

On Nov 12, 10:47 pm, Karine D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> @lien = Lien.find(params[:id])
> 
> ```
> 
> text field (if ‘3’ is entered, the database contains 3000000 and the  
> user still sees ‘3’ when re-editing). But decimal values are truncated  
> (if ‘1.5’ is entered, the database contains 1000000 instead of 1500000).
> 
> I guess that’s because I defined my cpc column as integer in the first  
> place, can I tell Rails to not truncate decimal values as I will use  
> them, or should I have gone another way to make this work properly?
> 
> Thanks,  
> Karine

Then don’t use update\_attributes. Set your attributes one by one.
