Sha256: d651d0e891c9e6e3da254a294b986604df095e5a425a65a53697013941ae9f03

Contents?: true

Size: 1.36 KB

Versions: 10

Compression:

Stored size: 1.36 KB

Contents

---
sidebar_position: 5
title: Merging searches
---

To find records that match multiple searches, it's possible to merge all the ransack search conditions into an ActiveRecord relation to perform a single query. In order to avoid conflicts between joined table names it's necessary to set up a shared context to track table aliases used across all the conditions before initializing the searches:

```ruby
shared_context = Ransack::Context.for(Person)

search_parents = Person.ransack(
  { parent_name_eq: "A" }, context: shared_context
)

search_children = Person.ransack(
  { children_name_eq: "B" }, context: shared_context
)

shared_conditions = [search_parents, search_children].map { |search|
  Ransack::Visitor.new.accept(search.base)
}

Person.joins(shared_context.join_sources)
  .where(shared_conditions.reduce(&:or))
  .to_sql
```
Produces:
```sql
SELECT "people".*
FROM "people"
LEFT OUTER JOIN "people" "parents_people"
  ON "parents_people"."id" = "people"."parent_id"
LEFT OUTER JOIN "people" "children_people"
  ON "children_people"."parent_id" = "people"."id"
WHERE (
  ("parents_people"."name" = 'A' OR "children_people"."name" = 'B')
  )
ORDER BY "people"."id" DESC
```

Admittedly this is not as simple as it should be, but it's workable for now. (Implementing [issue 417](https://github.com/activerecord-hackery/ransack/issues/417) could make this more straightforward.)

Version data entries

10 entries across 9 versions & 2 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/ransack-4.2.1/docs/docs/going-further/merging-searches.md
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/ransack-4.2.1/docs/docs/going-further/merging-searches.md
ransack-4.2.1 docs/docs/going-further/merging-searches.md
ransack-4.2.0 docs/docs/going-further/merging-searches.md
ransack-4.1.1 docs/docs/going-further/merging-searches.md
ransack-4.1.0 docs/docs/going-further/merging-searches.md
ransack-4.0.0 docs/docs/going-further/merging-searches.md
ransack-3.2.1 docs/docs/going-further/merging-searches.md
ransack-3.2.0 docs/docs/going-further/merging-searches.md
ransack-3.1.0 docs/docs/going-further/merging-searches.md