diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2018-11-12 03:39:28 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2018-11-12 03:39:28 +0900 |
commit | a741208f80dd33420a56486bd9ed2b0b9862234a (patch) | |
tree | d718f93e317084e299379b134135d600520150ea | |
parent | 44bee7f242da1df49a31416b762e0a03467ae2bb (diff) | |
download | rails-a741208f80dd33420a56486bd9ed2b0b9862234a.tar.gz rails-a741208f80dd33420a56486bd9ed2b0b9862234a.tar.bz2 rails-a741208f80dd33420a56486bd9ed2b0b9862234a.zip |
Ensure casting by decimal attribute when querying
Related 34cc301f03aea2e579d6687a9ea9782afc1089a0.
`QueryAttribute#value_for_database` calls only `type.serialize`, and
`Decimal#serialize` is a no-op unlike other attribute types.
Whether or not `serialize` will invoke `cast` is undefined in our test
cases, but it actually does not work properly unless it does so for now.
-rw-r--r-- | activemodel/lib/active_model/type/decimal.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/numeric_data_test.rb | 10 |
2 files changed, 12 insertions, 2 deletions
diff --git a/activemodel/lib/active_model/type/decimal.rb b/activemodel/lib/active_model/type/decimal.rb index e8ee18c00e..b37dad1c41 100644 --- a/activemodel/lib/active_model/type/decimal.rb +++ b/activemodel/lib/active_model/type/decimal.rb @@ -12,6 +12,10 @@ module ActiveModel :decimal end + def serialize(value) + cast(value) + end + def type_cast_for_schema(value) value.to_s.inspect end diff --git a/activerecord/test/cases/numeric_data_test.rb b/activerecord/test/cases/numeric_data_test.rb index 304714979c..079e664ee4 100644 --- a/activerecord/test/cases/numeric_data_test.rb +++ b/activerecord/test/cases/numeric_data_test.rb @@ -24,7 +24,10 @@ class NumericDataTest < ActiveRecord::TestCase ) assert m.save - m1 = NumericData.find(m.id) + m1 = NumericData.find_by( + bank_balance: 1586.43, + big_bank_balance: BigDecimal("1000234000567.95") + ) assert_kind_of Integer, m1.world_population assert_equal 2**62, m1.world_population @@ -48,7 +51,10 @@ class NumericDataTest < ActiveRecord::TestCase ) assert m.save - m1 = NumericData.find(m.id) + m1 = NumericData.find_by( + bank_balance: 1586.43122334, + big_bank_balance: BigDecimal("234000567.952344") + ) assert_kind_of Integer, m1.world_population assert_equal 2**62, m1.world_population |