Skip to content

Commit

Permalink
fix ActiveModel::RangeError when using multiple search fields
Browse files Browse the repository at this point in the history
  • Loading branch information
kha-wogi committed Sep 10, 2024
1 parent 2d56e78 commit dd33065
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
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

0 comments on commit dd33065

Please sign in to comment.