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

Index search is scoped to models default_scope #975

Open
aliaksandrb opened this issue Dec 19, 2024 · 0 comments
Open

Index search is scoped to models default_scope #975

aliaksandrb opened this issue Dec 19, 2024 · 0 comments

Comments

@aliaksandrb
Copy link

aliaksandrb commented Dec 19, 2024

Hi, thanks for the gem!

Let's say we have records that could be soft deleted (or any other default grouping property), and we want to search all records, but hide deleted by default:

class User < ApplicationRecord
  default_scope -> { where(deleted: false) }
end
class UserIndex < Chewy::Index
  index_scope User.unscoped

  field :deleted, type: "boolean"
end

With such setup, search by deleted: false works as expected ✅

Though, search by deleted: true doesn't work, because ActiveRecord adapter most of the time uses target.connection for queries, where target determined from relation.klass, here:

if class_or_relation.is_a?(relation_class)

So, while response from Elastic has hits for such query (deleted: true), afterwards Chewy queries model directly with ids being found, which guarantee to return empty dataset every time (because of contradiction: we search for deleted, while querying only not deleted), ex:

User.where(id: ids_found_for_deleted_records_from_elastic_hits) # <= but User is scoped!

This looks like a bug for me, as we probably should query the same scope as we provide to index_scope method (everything what is indexed supposed to be searchable).

A little workaround is possible by extracting search results ids re-query them manually, like, but I would assume it should be a default behavior.

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