Skip to content
Browse files

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
  • Loading branch information...
1 parent 756188b commit c147dd7ddd206f6c2d80d8957ee9fa575b00b13c @trisweb trisweb committed with rafaelfranca Jan 6, 2013
View
7 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.
View
6 activerecord/lib/active_record/connection_adapters/column.rb
@@ -175,11 +175,7 @@ def value_to_integer(value)
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
View
6 activerecord/test/cases/column_test.rb
@@ -56,6 +56,12 @@ def test_type_cast_object_without_to_i_to_integer
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

0 comments on commit c147dd7

Please sign in to comment.
Something went wrong with that request. Please try again.