ransack icon indicating copy to clipboard operation
ransack copied to clipboard

Make searches chainable using and/or

Open avit opened this issue 11 years ago • 4 comments

I'd like to add a feature for merging searches like search_parents.or(search_children). With #412 it becomes doable by setting up a shared context to handle the join aliases before building the search conditions:

shared_context = Ransack::Context.for(Person)
search_parents = Person.search({parent_name_eq: "A"}, context: shared_context)
search_children = Person.search({children_name_eq: "B"}, context: shared_context)
shared_conditions = [search_parents, search_children].map { |search|
  Ransack::Visitor.new.accept(search.base)
}.reduce(&:or)

Person.joins(shared_context.join_sources).where(shared_conditions)

Currently the context needs to be passed around before initializing searches, because the initializer automatically calls build to populate the condition tree. This would need to change to evaluate lazily.

avit avatar Aug 15 '14 17:08 avit

I unfortunately cannot use this at all. Ransack::Visitor.new.accept just returns nil.

art-solopov avatar Mar 14 '23 19:03 art-solopov

+1

imtiazwazir avatar Jul 02 '23 23:07 imtiazwazir

+1

Lance-Foley avatar Apr 09 '24 13:04 Lance-Foley