diff options
author | Tristan Harward <trisweb@gmail.com> | 2013-01-06 15:39:09 -0500 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-01-06 18:21:36 -0300 |
commit | c147dd7ddd206f6c2d80d8957ee9fa575b00b13c (patch) | |
tree | 58703e1075f81bdaf8c5aa75b558b9d0b10c8255 /activerecord | |
parent | 756188b512ca11e24262a74856e3bc11b9b2dbc9 (diff) | |
download | rails-c147dd7ddd206f6c2d80d8957ee9fa575b00b13c.tar.gz rails-c147dd7ddd206f6c2d80d8957ee9fa575b00b13c.tar.bz2 rails-c147dd7ddd206f6c2d80d8957ee9fa575b00b13c.zip |
Fix error when assigning NaN to an integer column
Also covers any non-castable case by returning nil, which
is in-line with the intention of the former implementation,
but covers the odd cases which respond to to_i but raise
an error when it's called, such as NaN, Infinity and -Infinity.
Fixes #8757
Backport of #8781
Conflicts:
activerecord/CHANGELOG.md
activerecord/test/cases/column_test.rb
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 7 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/column.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/column_test.rb | 6 |
3 files changed, 11 insertions, 8 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 1606b1a9f6..495390d16c 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,9 +1,10 @@ ## Rails 3.2.11 (unreleased) -* Fix undefined method `to_i` when calling `new` on a scope that uses an Array. - Fixes #8718. +* Fix undefined method `to_i` when calling `new` on a scope that uses an + Array; Fix FloatDomainError when setting integer column to NaN. + Fixes #8718, #8734, #8757. - *Jason Stirk* + *Jason Stirk + Tristan Harward* * Serialized attributes can be serialized in integer columns. Fix #8575. diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb index b93a728a39..e22d9f7222 100644 --- a/activerecord/lib/active_record/connection_adapters/column.rb +++ b/activerecord/lib/active_record/connection_adapters/column.rb @@ -175,11 +175,7 @@ module ActiveRecord when TrueClass, FalseClass value ? 1 : 0 else - if value.respond_to?(:to_i) - value.to_i - else - nil - end + value.to_i rescue nil end end diff --git a/activerecord/test/cases/column_test.rb b/activerecord/test/cases/column_test.rb index 8017a49827..77647d7986 100644 --- a/activerecord/test/cases/column_test.rb +++ b/activerecord/test/cases/column_test.rb @@ -56,6 +56,12 @@ module ActiveRecord column = Column.new("field", nil, "integer") assert_nil column.type_cast(Object.new) end + + def test_type_cast_nan_and_infinity_to_integer + column = Column.new("field", nil, "integer") + assert_nil column.type_cast(Float::NAN) + assert_nil column.type_cast(1.0/0.0) + end end end end |