surjay
surjay

Reputation: 123

How do you eager-load records with Elasticsearch::Model.search() across multiple models?

Using the elasticsearch-rails/elasticsearch-model gems.

I know you can do the following with the proxy on a single model:

MyModel.__elasticsearch__.search(search_hash).records.includes(:my_association)

I need to search across similar models which contain the same association I want to eager-load. The following errors out though with 'undefined method'.

Elasticsearch::Model.search(search_hash, [MyModel, MyOtherModel]).records.includes(:my_association)

Both .records calls seem to return an Elasticsearch::Model::Response::Records object, but it seems that .includes is only valid with a single model through the ES proxy.

Is there a way to easily do this when searching across multiple models?

Upvotes: 4

Views: 405

Answers (1)

Nicholas
Nicholas

Reputation: 176

Use a delegate. Here's an example from dimroc: Eager Loading Delegate

Upvotes: 0

Related Questions