Skip to content

Commit dd33065

Browse files
committed
fix ActiveModel::RangeError when using multiple search fields
1 parent 2d56e78 commit dd33065

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

lib/ransack/nodes/condition.rb

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -284,33 +284,33 @@ def negative?
284284
end
285285

286286
def arel_predicate
287-
predicate = attributes.map { |attribute|
287+
attributes.map { |attribute|
288288
association = attribute.parent
289-
if negative? && attribute.associated_collection?
290-
query = context.build_correlated_subquery(association)
291-
context.remove_association(association)
292-
if self.predicate_name == 'not_null' && self.value
293-
query.where(format_predicate(attribute))
294-
Arel::Nodes::In.new(context.primary_key, Arel.sql(query.to_sql))
295-
else
296-
query.where(format_predicate(attribute).not)
297-
Arel::Nodes::NotIn.new(context.primary_key, Arel.sql(query.to_sql))
298-
end
299-
else
300-
format_predicate(attribute)
289+
predicate = if negative? && attribute.associated_collection?
290+
query = context.build_correlated_subquery(association)
291+
context.remove_association(association)
292+
if self.predicate_name == 'not_null' && self.value
293+
query.where(format_predicate(attribute))
294+
Arel::Nodes::In.new(context.primary_key, Arel.sql(query.to_sql))
295+
else
296+
query.where(format_predicate(attribute).not)
297+
Arel::Nodes::NotIn.new(context.primary_key, Arel.sql(query.to_sql))
298+
end
299+
else
300+
format_predicate(attribute)
301+
end
302+
303+
if replace_right_node?(predicate)
304+
# Replace right node object to plain integer value in order to avoid
305+
# ActiveModel::RangeError from Arel::Node::Casted.
306+
# The error can be ignored here because RDBMSs accept large numbers
307+
# in condition clauses.
308+
plain_value = predicate.right.value
309+
predicate.right = plain_value
301310
end
302-
}.reduce(combinator_method)
303311

304-
if replace_right_node?(predicate)
305-
# Replace right node object to plain integer value in order to avoid
306-
# ActiveModel::RangeError from Arel::Node::Casted.
307-
# The error can be ignored here because RDBMSs accept large numbers
308-
# in condition clauses.
309-
plain_value = predicate.right.value
310-
predicate.right = plain_value
311-
end
312-
313-
predicate
312+
predicate
313+
}.reduce(combinator_method)
314314
end
315315

316316
private

spec/ransack/predicate_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ module Ransack
4242
field = "#{quote_table_name("people")}.#{quote_column_name("salary")}"
4343
expect(@s.result.to_sql).to match /#{field} = #{val}/
4444
end
45+
46+
it 'generates a condition for multiple fields with a huge integer value' do
47+
val = 123456789012345678901
48+
@s.salary_or_articles_title_eq = val
49+
person_field = "#{quote_table_name("people")}.#{quote_column_name("salary")}"
50+
article_field = "#{quote_table_name("articles")}.#{quote_column_name("title")}"
51+
expect(@s.result.to_sql).to match /#{person_field} = #{val}/
52+
expect(@s.result.to_sql).to match /#{article_field} = '#{val}'/
53+
end
4554
end
4655

4756
describe 'lteq' do

0 commit comments

Comments
 (0)