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

fix ActiveModel::RangeError when using multiple search fields #1523

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions lib/ransack/nodes/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,33 +284,33 @@ def negative?
end

def arel_predicate
predicate = attributes.map { |attribute|
attributes.map { |attribute|
association = attribute.parent
if negative? && attribute.associated_collection?
query = context.build_correlated_subquery(association)
context.remove_association(association)
if self.predicate_name == 'not_null' && self.value
query.where(format_predicate(attribute))
Arel::Nodes::In.new(context.primary_key, Arel.sql(query.to_sql))
else
query.where(format_predicate(attribute).not)
Arel::Nodes::NotIn.new(context.primary_key, Arel.sql(query.to_sql))
end
else
format_predicate(attribute)
predicate = if negative? && attribute.associated_collection?
query = context.build_correlated_subquery(association)
context.remove_association(association)
if self.predicate_name == 'not_null' && self.value
query.where(format_predicate(attribute))
Arel::Nodes::In.new(context.primary_key, Arel.sql(query.to_sql))
else
query.where(format_predicate(attribute).not)
Arel::Nodes::NotIn.new(context.primary_key, Arel.sql(query.to_sql))
end
else
format_predicate(attribute)
end

if replace_right_node?(predicate)
# Replace right node object to plain integer value in order to avoid
# ActiveModel::RangeError from Arel::Node::Casted.
# The error can be ignored here because RDBMSs accept large numbers
# in condition clauses.
plain_value = predicate.right.value
predicate.right = plain_value
end
}.reduce(combinator_method)

if replace_right_node?(predicate)
# Replace right node object to plain integer value in order to avoid
# ActiveModel::RangeError from Arel::Node::Casted.
# The error can be ignored here because RDBMSs accept large numbers
# in condition clauses.
plain_value = predicate.right.value
predicate.right = plain_value
end

predicate
predicate
}.reduce(combinator_method)
end

private
Expand Down
9 changes: 9 additions & 0 deletions spec/ransack/predicate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ module Ransack
field = "#{quote_table_name("people")}.#{quote_column_name("salary")}"
expect(@s.result.to_sql).to match /#{field} = #{val}/
end

it 'generates a condition for multiple fields with a huge integer value' do
val = 123456789012345678901
@s.salary_or_articles_title_eq = val
person_field = "#{quote_table_name("people")}.#{quote_column_name("salary")}"
article_field = "#{quote_table_name("articles")}.#{quote_column_name("title")}"
expect(@s.result.to_sql).to match /#{person_field} = #{val}/
expect(@s.result.to_sql).to match /#{article_field} = '#{val}'/
end
end

describe 'lteq' do
Expand Down
Loading