Skip to content

Model.import not respecting default_scope #784

Closed
@ctrlaltdylan

Description

@ctrlaltdylan

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions