Skip to content

Model.import not respecting default_scope #784

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

Closed
ctrlaltdylan opened this issue Apr 5, 2018 · 6 comments
Closed

Model.import not respecting default_scope #784

ctrlaltdylan opened this issue Apr 5, 2018 · 6 comments

Comments

@ctrlaltdylan
Copy link

ctrlaltdylan commented Apr 5, 2018

My model has a default_scope like this:

default_scope { where(status: 1).where.not(user: nil) }

However, during searches Elasticsearch will return response.records that do not actually respect the scope:

$ pry(VehicleElasticSearchService)> response.records.to_a.first
RvshareRv Load (3.2ms)  SELECT `default_rvs`.* FROM `default_rvs` WHERE (`vehicles`.`user_id` IS NOT NULL) AND `vehicles`.`status` = 1 AND `vehicles`.`id` IN (1004787, 1005459, 1005571, 1005785, 1006249, 1006326, 1006389, 1006427, 1006650, 1006667, 1006708, 1006802, 1006839, 1006877, 1006931, 114106, 114107, 114111, 114112, 114119, 114122, 114123, 114124, 114650, 115314)
=> nil

I ran the query without the WHERE status = 1 and was able to retrieve the records, so that means that Elasticsearch imported records outside of the defined default_scope

It seems that during indexing (Vehicle.import), the default_scope is not being applied and these records somehow make it into the index.

How can we force Elasticsearch to respect the default_scope of a model? Or conditionally import records?

@ctrlaltdylan ctrlaltdylan changed the title Model.import not respecting default_scope Model.import not respecting default_scope Apr 5, 2018
@estolfo
Copy link
Contributor

estolfo commented Aug 3, 2018

@ctrlaltdylan Would this change in a pull request fix the issue you're having?

@ctrlaltdylan
Copy link
Author

@estolfo honestly I'm not sure, I haven't looked into the internals much of ElasticSearch, I just wanted to at least report the bug. I'm sure you know better than I if it'll fix the issue!

@ctrlaltdylan
Copy link
Author

Also this bug was using the mysql2 Rails db adapter, not mongo.

@estolfo
Copy link
Contributor

estolfo commented Aug 6, 2018

ah, ok. Thanks for letting me know.

@estolfo
Copy link
Contributor

estolfo commented Aug 16, 2018

@ctrlaltdylan I've opened a pull request to address this. Please test it out and let me know if it works ok for you. Thanks!

Update: I've discovered that the default_scope was already applied so no code changes were needed. I've updated the PR to just be a test proving this. Please let me know if I'm missing something, otherwise, I'll consider this working.

@estolfo
Copy link
Contributor

estolfo commented Sep 5, 2018

I'm going to close this issue as I've merged in the pull request #827 testing this issue and showing that the default scope is indeed applied to an import. Please comment or open a new issue if it's not resolved.

@estolfo estolfo closed this as completed Sep 5, 2018
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

2 participants