I've just upgraded an old app from Rails 3.0 to 4.2 and now it is impossible to query the database for very long IDs.
Rails 3
Product.where(associated_id: 1311344470881970878875083923).first
=> nil
Rails 4.2.3
Product.where(associated_id: 1311344470881970878875083923).first
RangeError: 1311344470881970878875083923 is out of range for ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer with limit 8
from ~/.rvm/gems/ruby-2.2.1@gemset/gems/activerecord-4.2.3/lib/active_record/type/integer.rb:45:in `ensure_in_range'
I'd expect that the finder returns nothing in Rails 4.2.3 as well in such a case. It's just searching for results, we don't need to call ensure_in_range
.
UPDATE: however, the following works fine (it may serve as a workaround):
Product.find_by(associated_id: 1311344470881970878875083923)
It works because find_by
is implemented as below:
def find_by(*args)
where(*args).take
rescue RangeError
nil
end
http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find_by
Unfortunately a similar method for returning all results is missing in ActiveRecord 4.2.3
def find_all_by(*args)
where(*args).load
rescue RangeError
none
end