Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems searching for resources belonging to STI #180

Open
adamruzicka opened this issue Aug 13, 2018 · 0 comments
Open

Problems searching for resources belonging to STI #180

adamruzicka opened this issue Aug 13, 2018 · 0 comments

Comments

@adamruzicka
Copy link
Collaborator

Let's consider classes Location and Organization which are subclasses of Placement using STI. Class Item belongs_to one organization and one location. Searching for an Item belonging to organization and location generates wrong search query and always returns 0 items.

The search query first LEFT OUTER JOINs the placements, then LEFT OUTER JOINs the placements again with an alias, but when the filtering is done, the first joined table is used twice instead of each being used once.

# Running this
Item.search_for('location = Somewhere and organization = Something').count

# Generates this SQL
SELECT COUNT(DISTINCT "items"."id") FROM "items"
  LEFT OUTER JOIN "placements" ON "placements"."id" = "items"."location_id" AND "placements"."type" IN ('Location')
  LEFT OUTER JOIN "placements" "organizations_items" ON "organizations_items"."id" = "items"."organization_id" AND "organizations_items"."type" IN ('Organization')
     WHERE ((("placements"."title" = 'Somewhere') AND ("placements"."title" = 'Something')))

Example rails application demonstrating the issue can be found at adamruzicka/scoped_search_reproducer.

Versions used:

  • ruby-2.5.0
  • activerecord (5.2.1)
  • scoped_search (4.1.3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant