Ransack: Ransack filter "*_eq" does not work with large numbers

Created on 6 Sep 2019  路  10Comments  路  Source: activerecord-hackery/ransack

@gregmolnar

The latest version of ransack (2.3.0) with the latest version of Rails (6.0.0) this code below (with ransack filters *_eq) does not work:

# This code works.
Person.where(id: 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111).to_sql
#=> "SELECT \"people\".* FROM \"people\" WHERE 1=0"

# This code does not work.
Person.ransack(id_eq: 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111).result.to_sql
#=> ActiveModel::RangeError: 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 is out of range for ActiveModel::Type::Integer with limit 8 bytes from /usr/local/lib/ruby/gems/2.6.0/gems/activemodel-6.0.0/lib/active_model/type/integer.rb:40:in `ensure_in_range'

The first code snippet, in Rails 6, works because of this PR: https://github.com/rails/rails/pull/30000

Thanks! :+1:

Most helpful comment

@seanfcarroll In the application, the user can search for records by id. Then, it is possible user provides a long number 馃憤 can you reopen the issue?

All 10 comments

@marciojg @willradi

@gregmolnar 馃憤

That's a pretty big number @pedrofurtado !
Do you have a solution for this, please submit a PR if you do. If you don't please submit a PR with the failing test and I'll take a look at it.

@seanfcarroll Could you help us? Because I know the bug, but not the solution :laughing: :see_no_evil:
Once we have a PR created, I can help testing it in our application :+1:

It would be really helpful if you could point me to a basic Rails app where the problem exists.

Why is this needed urgently? Are you working with such large numbers?

@pedrofurtado can you explain why such a large number might be needed? This issue will be closed otherwise.

@seanfcarroll In the application, the user can search for records by id. Then, it is possible user provides a long number 馃憤 can you reopen the issue?

@seanfcarroll is there any workaround?

For anyone looking for a workaround, there is a monkey patch:

For rails 6.x:

##
# Monkey-patch.
#
# URLs for reference:
# https://github.com/activerecord-hackery/ransack/issues/1064
# https://github.com/kamipo/activerecord-suppress_range_error
# https://github.com/rails/rails/blob/master/activemodel/lib/active_model/type/integer.rb#L47-L52
module ActiveRecord
  module SuppressRangeError
    private

    def ensure_in_range(value)
      value
    end
  end

  module Type
    class Integer
      prepend SuppressRangeError
    end
  end
end

Person.ransack(id_eq: 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111).result.to_sql
# It works!

or a similar version for rails 6.x:

ActiveRecord::Type::Integer.class_eval do
  private

  def ensure_in_range(value)
    value
  end
end

Person.ransack(id_eq: 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111).result.to_sql
# It works!

For rails 5.x:

##
# Monkey-patch.
#
# URLs for reference:
# https://github.com/activerecord-hackery/ransack/issues/1064
# https://github.com/kamipo/activerecord-suppress_range_error
# https://github.com/rails/rails/blob/master/activemodel/lib/active_model/type/integer.rb#L47-L52
module ActiveRecord
  module SuppressRangeError
    private

    def ensure_in_range(value)
    end
  end

  module Type
    class Integer
      prepend SuppressRangeError
    end
  end
end

Person.ransack(id_eq: 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111).result.to_sql
# It works!

or a similar version for rails 5.x:

ActiveRecord::Type::Integer.class_eval do
  private

  def ensure_in_range(value)
  end
end

Person.ransack(id_eq: 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111).result.to_sql
# It works!

I also open a PR in https://github.com/kamipo/activerecord-suppress_range_error gem, that could provide this workaround to used in Rails 5/6 + Ransack 馃 :

https://github.com/kamipo/activerecord-suppress_range_error/pull/5

Was this page helpful?
0 / 5 - 0 ratings