diff options
author | joker1007 <kakyoin.hierophant@gmail.com> | 2014-07-29 20:16:10 +0900 |
---|---|---|
committer | joker1007 <kakyoin.hierophant@gmail.com> | 2014-08-01 00:29:20 +0900 |
commit | 1d6a87779c73bc7d9d5f235afadc0df4a36f534f (patch) | |
tree | b7652dd8079e2481cd6ef9e172d41704797e6819 /activerecord/lib/active_record | |
parent | 84093c662770893ad840c36f2b99204593d4a7de (diff) | |
download | rails-1d6a87779c73bc7d9d5f235afadc0df4a36f534f.tar.gz rails-1d6a87779c73bc7d9d5f235afadc0df4a36f534f.tar.bz2 rails-1d6a87779c73bc7d9d5f235afadc0df4a36f534f.zip |
Fix type casting to Decimal from Float with ...
When I defines large precision column at RDBMS,
I assigns float value, raise ArgumentError (precision too large).
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/type/decimal.rb | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/type/decimal.rb b/activerecord/lib/active_record/type/decimal.rb index ba5d244729..d10778eeb6 100644 --- a/activerecord/lib/active_record/type/decimal.rb +++ b/activerecord/lib/active_record/type/decimal.rb @@ -14,12 +14,25 @@ module ActiveRecord private def cast_value(value) - if value.is_a?(::Numeric) || value.is_a?(::String) + case value + when ::Float + BigDecimal(value, float_precision) + when ::Numeric, ::String BigDecimal(value, precision.to_i) - elsif value.respond_to?(:to_d) - value.to_d else - cast_value(value.to_s) + if value.respond_to?(:to_d) + value.to_d + else + cast_value(value.to_s) + end + end + end + + def float_precision + if precision.to_i > ::Float::DIG + 1 + ::Float::DIG + 1 + else + precision.to_i end end end |