Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveRecord::QueryMethods#in_order_of error when passing an out-of-range Integer #44745

Open
tejanium opened this issue Mar 22, 2022 · 1 comment · May be fixed by #44746
Open

ActiveRecord::QueryMethods#in_order_of error when passing an out-of-range Integer #44745

tejanium opened this issue Mar 22, 2022 · 1 comment · May be fixed by #44746

Comments

@tejanium
Copy link

tejanium commented Mar 22, 2022

Steps to reproduce

When we pass an out-of-range integer to in_order_of, it will raise an ActiveModel::RangeError error.

[1] pry(main)> Post.in_order_of(:id, [1, 9999999999999])
ActiveModel::RangeError: 9999999999999 is out of range for ActiveModel::Type::Integer with limit 4 bytes

[2] pry(main)> Post.in_order_of(:id, ["1", "9999999999999"])
ActiveModel::RangeError: 9999999999999 is out of range for ActiveModel::Type::Integer with limit 4 bytes

Below is the runnable script

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", github: "rails/rails", branch: "main"
  gem "sqlite3"
end

require "active_record"
require "minitest/autorun"
require "logger"

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
  end
end

class Post < ActiveRecord::Base
end

class BugTest < Minitest::Test
  # Fail
  def test_out_of_range
    post = Post.create!
    out_of_range_id = Post.type_for_attribute(:id).send(:max_value)

    assert_equal Post.in_order_of(:id, [post.id, out_of_range_id]).map(&:id), [post.id]
  end

  # Success
  def test_where_with_out_of_range
    post = Post.create!
    out_of_range_id = Post.type_for_attribute(:id).send(:max_value)

    assert_equal Post.where(id: [post.id, out_of_range_id]).map(&:id), [post.id]
  end

  def test_with_invalid_ids
    post = Post.create!

    assert_equal Post.where(id: [post.id, -1, :foo, "bar"]).map(&:id), [post.id]
  end
end

Expected behavior

The Enumerable version will ignore it

[3] pry(main)> [ Post.find(5), Post.find(3), Post.find(1) ].in_order_of(:id, [ 1, 5, 9999999999999 ]).map(&:id)
=> [1, 5]

so does where

[4] pry(main)> Post.where(id: [1, 9999999999999]).map(&:id)
=> [1]

and in_order_of with non-existent value

[5] pry(main)> Post.in_order_of(:id, [1, -1, :foo, "bar"]).map(&:id)
=> [1]

therefore, in_order_of should ignore out-of-range value

Actual behavior

Raise an ActiveModel::RangeError error

System configuration

Rails version: 7.0.2.3

Ruby version: 2.7.5p203

@rails-bot
Copy link

rails-bot bot commented Jun 22, 2022

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 7-0-stable branch or on main, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

@rails-bot rails-bot bot added the stale label Jun 22, 2022
@rails-bot rails-bot bot closed this as completed Jun 29, 2022
@skipkayhil skipkayhil reopened this Jun 29, 2022
@rails-bot rails-bot bot removed the stale label Jun 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants