diff --git a/lib/ransack/nodes/condition.rb b/lib/ransack/nodes/condition.rb index af69c037..248638d2 100644 --- a/lib/ransack/nodes/condition.rb +++ b/lib/ransack/nodes/condition.rb @@ -339,7 +339,7 @@ def in_predicate?(predicate) end def casted_array?(predicate) - predicate.value.is_a?(Array) && predicate.is_a?(Arel::Nodes::Casted) + predicate.is_a?(Arel::Nodes::Casted) && predicate.value.is_a?(Array) end def format_values_for(predicate) diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index d41cd6d0..ebc54a4c 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -408,6 +408,15 @@ def self.simple_escaping? expect(s.result.map(&:id)).to eq [3, 2, 1] end + it 'should function correctly with HABTM associations' do + article = Article.first + tag = article.tags.first + s = Person.ransack(article_tags_in: [tag.id]) + + expect(s.result.count).to be 1 + expect(s.result.map(&:id)).to eq [article.person.id] + end + it 'should function correctly when passing an array of strings' do a, b = Person.select(:id).order(:id).limit(2).map { |a| a.id.to_s } diff --git a/spec/support/schema.rb b/spec/support/schema.rb index 555299c4..1000cca0 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -126,6 +126,17 @@ class Person < ApplicationRecord Arel.sql(query) end + ransacker :article_tags, formatter: proc { |id| + if Tag.exists?(id) + joins(articles: :tags) + .where(tags: { id: id }) + .distinct + .select(:id).arel + end + } do |parent| + parent.table[:id] + end + def self.ransackable_attributes(auth_object = nil) if auth_object == :admin authorizable_ransackable_attributes - ['only_sort']