Forum: Ruby on Rails BigDecimal column type?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2006-04-01 03:54
I was hoping 1.1 would provide a way to map SQL "decimal" column types
to BigDecimal values, since BigDecimal is part of the Ruby standard api
as of 1.8.

But I don't see any support for that in the migration documentation, and
when I try to let Active Record do the mapping for me, it seems to
coerce the column into a Float (as the AwdwR book says).

Is it really there in 1.1 but I just don't know what to do to tap into
it?  Or is it still not part of ActiveRecord yet?

I've heard about Tobias' "Money" plug-in but I'm not sure I want to go
that route unless I have to.

Storing money values should be very common - any shopping-cart style web
app will need to - but using floats can give rounding errors that can be
hard to spot (unlike the decimal type).

Any ideas?  Maybe I'm missing something obvious?

Thanks!
Jeff
A90204c955db033cd975f7bb0ec9600b?d=identicon&s=25 Ashley Moran (Guest)
on 2006-04-01 19:08
(Received via mailing list)
On Apr 01, 2006, at 2:54 am, Jeff Cohen wrote:
> I was hoping 1.1 would provide a way to map SQL "decimal" column types
> to BigDecimal values, since BigDecimal is part of the Ruby standard
> api
> as of 1.8.

Jeff

I missed this feature too - in fact it made Rails such a non-starter
at my company that I wrote a patch for ActiveRecord, which is
available at http://dev.rubyonrails.org/ticket/4274 .  I wrote it
initially for Postgres and ported it to SQL Server (we are moving
from SQL Server to Postgres at work so they are the two databases I'm
familiar with).  If you use either of these perhaps you'd give it a
try and let me know what you think.  If I get time over the next few
weeks I'll port it to Firebird and then MySQL.  But really, you want
to be using Postgres for serious work :)

Ashley
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2006-04-01 21:13
Ashley Moran wrote:
> If I get time over the next few
> weeks I'll port it to Firebird and then MySQL.  But really, you want
> to be using Postgres for serious work :)
>

Hi Ashley,

Unfortunately I only use MySQL currently.  If you do get a chance to
port it, just send an email to this list or to me directly and I'll give
it a whirl.

Thanks a lot,
Jeff
www.softiesonrails.com
A90204c955db033cd975f7bb0ec9600b?d=identicon&s=25 Ashley Moran (Guest)
on 2006-04-04 13:57
(Received via mailing list)
On Saturday 01 April 2006 20:13, Jeff Cohen wrote:
> Hi Ashley,
>
> Unfortunately I only use MySQL currently.  If you do get a chance to
> port it, just send an email to this list or to me directly and I'll give
> it a whirl.
>
> Thanks a lot,
> Jeff
> www.softiesonrails.com

Had a look at the MySQL adapter and it should be easy to get the numeric
precision and scale out of the database.  If I'm not busy I'll have a
look at
it over the weekend.

Ashley
3d333b0012928f3dd5a6861cb09ad683?d=identicon&s=25 kris (Guest)
on 2006-04-04 15:47
Any news on this.. very much needed!

Ashley Moran wrote:
> On Saturday 01 April 2006 20:13, Jeff Cohen wrote:
>> Hi Ashley,
>>
>> Unfortunately I only use MySQL currently.  If you do get a chance to
>> port it, just send an email to this list or to me directly and I'll give
>> it a whirl.
>>
>> Thanks a lot,
>> Jeff
>> www.softiesonrails.com
>
> Had a look at the MySQL adapter and it should be easy to get the numeric
> precision and scale out of the database.  If I'm not busy I'll have a
> look at
> it over the weekend.
>
> Ashley
A90204c955db033cd975f7bb0ec9600b?d=identicon&s=25 Ashley Moran (Guest)
on 2006-04-12 11:51
(Received via mailing list)
On Tuesday 04 April 2006 14:47, kris wrote:
> 04/04/2006 14:47
>
> Any news on this.. very much needed!

Kris,

Last night I ported the decimal patch I've been working on to MySQL.  If
you
want to try it you will need to freeze edge or gem into your application
directory, then apply the patch.  Note that the PostgreSQL adapter has
recently been modified in the Rails trunk, so the patch will fail
against the
1.1 code (and I think 1.1.1 too).  But this should not affect you using
MySQL.

Also, I still have to add two lines to SchemaDumper so it will dump the
precision and scale of decimals.  This means that if you test an app
with
decimal columns, they will get the database default precision and scale.
I
didn't notice this at first because I use Postgres, and that defaults to
using the maximum values available.  However, SQL standard says the
scale
should default to 0, so you will end up with integers coming out of your
test
database.  Just a heads up.

Here is the patch:

http://dev.rubyonrails.org/ticket/4274

Let me know if you have any issues with it (or even better post on the
trac
page)

Ashley
646ed1f018c008e200d58588f08ba753?d=identicon&s=25 Angus McDonald (Guest)
on 2006-04-12 13:14
(Received via mailing list)
Ashley,

Cool! This answers my previous issue!

Angus
A90204c955db033cd975f7bb0ec9600b?d=identicon&s=25 Ashley Moran (Guest)
on 2006-04-13 12:14
(Received via mailing list)
On Wednesday 12 April 2006 12:13, Angus McDonald wrote:
> Ashley,
>
> Cool! This answers my previous issue!
>
> Angus

I've modified the patch to work as a plugin, if anyone is interested.
You can
download it here: http://www.ashleymoran.me.uk/sql_decimal_support.tgz

The patch has a full set of unit tests, but the plugin has none.  I've
tried
it out with Postgres and it seems ok.  SQL Server and MySQL may not work
if
I've copied something wrong.

Ashley
This topic is locked and can not be replied to.