# Decimal item gets truncated at DB-storage time

**URL:** <https://www.ruby-forum.com/t/decimal-item-gets-truncated-at-db-storage-time/185083>\
**Category:** Rails\
**Created:** [March 13, 2010, 4:45am UTC](https://www.ruby-forum.com/t/decimal-item-gets-truncated-at-db-storage-time/185083 "2010-03-13T04:45:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [March 13, 2010, 4:45am UTC](https://www.ruby-forum.com/t/decimal-item-gets-truncated-at-db-storage-time/185083/1 "2010-03-13T04:45:27Z")

</div>

Hi All,

I created a new Expense item. In particular, the value I entered in  
the Amount field was 14.99. In the scaffold invocation for this  
field, I declared its type to be Decimal.

The log for the creation of this expense record shows  
“amount”=\>“14.99”

But log for the SQL statement shows:  
Create (16.0ms) [0m [0mINSERT INTO `expenses` (`purpose`, `by`,  
`updated_at`, `date`, `type`, `amount`, `vendor`, `created_at`)  
VALUES(‘fun’, ‘RLM’, ‘2010-03-12 22:11:24’, ‘2010-03-12’, NULL, 14,  
‘Ringling Bros’, ‘2010-03-12 22:11:24’)  
So the amount has been converted to an integer with the decimal  
portion of the input truncated.

What’s the best way to resolve this problem?

Thanks in Advance,  
Richard

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [March 13, 2010, 5:05am UTC](https://www.ruby-forum.com/t/decimal-item-gets-truncated-at-db-storage-time/185083/2 "2010-03-13T05:05:56Z")

</div>

On Fri, 2010-03-12 at 19:44 -0800, RichardOnRails wrote:

> Create (16.0ms) [0m [0mINSERT INTO `expenses` (`purpose`, `by`,  
> `updated_at`, `date`, `type`, `amount`, `vendor`, `created_at`)  
> VALUES(‘fun’, ‘RLM’, ‘2010-03-12 22:11:24’, ‘2010-03-12’, NULL, 14,  
> ‘Ringling Bros’, ‘2010-03-12 22:11:24’)  
> So the amount has been converted to an integer with the decimal  
> portion of the input truncated.
> 
> What’s the best way to resolve this problem?
> 
> Thanks in Advance,

* * *

check the sql db and see how the column is defined…  
check db/schema.rb and see how the column is defined…

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [March 13, 2010, 6:36am UTC](https://www.ruby-forum.com/t/decimal-item-gets-truncated-at-db-storage-time/185083/3 "2010-03-13T06:36:03Z")

</div>

Hi Craig,

Thanks for responding.

sql.db  
There doesn’t appear to be such a critter in my project, although it  
was created with script/scaffold

db\schema.db  
create\_table “expenses”, :force =\> true do |t|  
[snip]  
t.integer “amount”, :limit =\> 10, :precision =\> 10, :scale =\>  
0

But just before I logged in right now, the fundamental cause of my  
problem struck me:  
When I declared expense:decimal in the scaffold, I didn’t  
declare :scale=\>10, :precision=\>2, which is what I want. So there’s a  
new question, I think:

- Can I declare successive migrations where I drop column and then add  
column, or
- can I just add those attributes to the column?

I don’t care about the any data in there at the moment: it’s all toy  
data for testing.

I look forward to your additional thoughts.

Best wishes,  
Richard

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [March 13, 2010, 7:32am UTC](https://www.ruby-forum.com/t/decimal-item-gets-truncated-at-db-storage-time/185083/4 "2010-03-13T07:32:14Z")

</div>

On Fri, 2010-03-12 at 21:34 -0800, RichardOnRails wrote:

> ```
> [snip]
> 
> ```
> 
> - can I just add those attributes to the column?
> 
> I don’t care about the any data in there at the moment: it’s all toy  
> data for testing.
> 
> I look forward to your additional thoughts.

* * *

1 - When I said SQL DB, I meant the actual SQL Database that you are  
using. i.e. MySQL, SQLite, PostgreSQL, etc. Do you know how to look at  
your database?

2 - Get familiar with Rails API…

```
http://api.rubyonrails.org/

It has answers to so many things that you can look up for reference

```

and in this case, you probably want to find ActiveRecord::Migrations and  
you will find that yes, you can drop/add columns and you can change  
columns via migrations.

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [March 13, 2010, 6:27pm UTC](https://www.ruby-forum.com/t/decimal-item-gets-truncated-at-db-storage-time/185083/5 "2010-03-13T18:27:42Z")

</div>

Thanks, again, profusely for your response.

\< Do you know how to look at your database?

mysql\> describe expenses;  
±-----------±--------------  
| Field | Type  
±-----------±--------------  
| amount | decimal(10,0)

> > Can I declare successive migrations where I drop column …  
> > Get familiar with Rails API… [http://api.rubyonrails.org/](http://api.rubyonrails.org/)

Thanks for that API. Actually, I’ve had a tab open on

> **[ActionView::Helpers::FormOptionsHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html)**
>
> Provides a number of methods for turning different kinds of containers into a set of option tags.

while trying to get a control with drop-down-selection coded … but  
that’s for another day.

> It has answers to so many things that you will find that yes,  
> you can drop/add columns and you can change columns via migrations.

Thank goodness for that! But I wasn’t able to find “drop column” and  
resorted to  
[http://www.oracle.com/technology/pub/articles/kern-rails-migrations.html](http://www.oracle.com/technology/pub/articles/kern-rails-migrations.html)  
to find this list:

13 remove\_column :albums, :year  
14 rename\_column :albums, :year\_int, :year  
15 change\_column :albums, :year, :integer, :limit =\> 4, :null =\>  
false

which gave me the answer I really wanted: #15

Bottom line: you’re a great resource. You pointed me in the right  
directions and I got to Nirvana!

Thanks again, Craig, very much,  
Richard
