BigDecimal still does not behave 100% correctly,
My relevant model fragment looks like this:
t.column :lat, :decimal, :precision => 20, :scale => 15, :null =>
false
t.column :lng, :decimal, :precision => 20, :scale => 15, :null =>
false
The controller calls a finder
find_by_lat_and_lng(@lat, @lng) which produces a log line:
SELECT * FROM geo_locations WHERE (geo_locations.lng
=
‘-70.85366249084473’ AND geo_locations.lat
= ‘42.66684871528651’)
LIMIT 1
This select returns nothing if ran manually but there is a record in the
table corresponding to the lng and lat criteria.
If I remove quites around lat and lng values then it works and returns
one record (as it should be), like this:
SELECT * FROM geo_locations WHERE (geo_locations.lng
=
-70.85366249084473 AND geo_locations.lat
= 42.66684871528651) LIMIT 1
Is there a way to tell rails to send decimal values without quites (I’m
assuming that quoted values are treated as strings and that’s why they
don’t match).
Any ideas?
When the finder parameters converted to float like this
find_by_lat_and_lng(@lat.to_f, @lng.to_f) then it does send the
parameters into the query non quoted. The problem is that the float does
not have enough precision (at least not in my case) and the finder still
does not return the value. When the decimal values are used in the
finder like this find_by_lat_and_lng(BigDecimal.new(@lat),
BigDecimal.new(@lng)) the query does quote the parameters.
So, my question is: shouldn’t BigDecimal behave similar to Float in this
regard, after all they both numbers. Seems like a bug to me.
If anyone has any advise how to deal with this situation, would be
highly appreciated.
thanks
The question is if LongDecimal (gem-name: long-decimal) would not be the
better match for database fields than
BigDecimal, which is more or less a Float with the capability to have
more digits.
Best regards,
Karl
Karl B. wrote:
The question is if LongDecimal (gem-name: long-decimal) would not be the
better match for database fields than
BigDecimal, which is more or less a Float with the capability to have
more digits.
Best regards,
Karl
Karl,
thank you for you reply,
the precision is not really a problem for me – the BigDecimal fits my
needs just fine. The problem is in active record that does not handle
numbers other then Floats correctly when constructing select queries –
it puts the parameters in quotes which it should not do. I’m afraid that
LongDecimal would not be any different in this regard for active record
(correct me if I’m wrong) thus switching to it would not really help me
much,
thank you, let me know if you have any other suggestions…
Dmitry