Skip to content

Instantly share code, notes, and snippets.

@davetapley
Last active December 30, 2015 23:49
Show Gist options
  • Save davetapley/7903686 to your computer and use it in GitHub Desktop.
Save davetapley/7903686 to your computer and use it in GitHub Desktop.
FloatDomainError: Infinity when updating PostgreSQL range with infinity (ActiveRecord 4.0.x) and PostgreSQL (9.2)
1) Error:
RangeTest#test:
FloatDomainError: Infinity
/home/dave/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.1/lib/active_record/attribute_methods/dirty.rb:97:in `to_r'
/home/dave/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.1/lib/active_record/attribute_methods/dirty.rb:97:in `=='
/home/dave/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.1/lib/active_record/attribute_methods/dirty.rb:97:in `=='
/home/dave/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.1/lib/active_record/attribute_methods/dirty.rb:97:in `!='
/home/dave/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.1/lib/active_record/attribute_methods/dirty.rb:97:in `_field_changed?'
/home/dave/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.1/lib/active_record/attribute_methods/dirty.rb:66:in `write_attribute'
/home/dave/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.1/lib/active_record/attribute_methods/write.rb:21:in `__temp__e657d6f52716e67656='
/home/dave/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.1/lib/active_record/persistence.rb:218:in `update_attribute'
test.rb:42:in `test'
gem 'activerecord', '5.0.1'
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Be sure to: $ createdb postgresql_ranges_infinity_issue
ActiveRecord::Base.establish_connection(adapter: 'postgresql', database: 'postgresql_ranges_infinity_issue')
ActiveRecord::Base.logger = Logger.new(STDOUT)
class CreatePostgressqlRanges < ActiveRecord::Migration
def change
create_table :postgresql_ranges do |t|
t.numrange :num_range
end
end
end
# Comment this out once you've run it the first time
CreatePostgressqlRanges.new.migrate :up
class PostgresqlRange < ActiveRecord::Base
end
class RangeTest < MiniTest::Unit::TestCase
def test
# We can create a range successfully
range = PostgresqlRange.create num_range: (1000..Float::INFINITY)
assert_equal 1000, range.num_range.first
assert_equal Float::INFINITY, range.num_range.last
# And update it while we have the same object
range.update_attributes num_range: (1000..Float::INFINITY)
assert_equal 1000, range.num_range.first
assert_equal Float::INFINITY, range.num_range.last
# But if we get a new reference
_range = PostgresqlRange.find range.id
# Any attept to update yields FloatDomainError: Infinity
assert_raises(FloatDomainError) do
_range.update_attribute :num_range, (1000..Float::INFINITY)
end
assert_raises(FloatDomainError) do
_range.update_attributes num_range: (1000..Float::INFINITY)
end
assert_raises(FloatDomainError) do
_range.num_range = (1000..Float::INFINITY)
_range.save
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment