undefined method `each' for Ransack::Search when using Active Admin and Elastic Search? undefined method `each' for Ransack::Search when using Active Admin and Elastic Search? elasticsearch elasticsearch

undefined method `each' for Ransack::Search when using Active Admin and Elastic Search?


Your problem is that you use ElasticSearch and Ransack (wich is a dependency of ActiveAdmin) at the same Model.

Ransack has a protection for this case. You can search in Ransack with search and ransack, if the model has already a search method Ransack don't over write them.

You need to ensure that ElasticSearch is loaded before Ransack (ActiveAdmin), that should solve your problem and you can use your code like before adding ActiveAdmin.

The problem with the solution of @nicooga is, that it's use Ransack to search and not ElasticSearch.

EDIT: This should be the best solution:

If you use tire:

Shop.tire.search

(Source)

If you use elasticsearch-ruby or elasticsearch-rails:

Shop.__elasticsearch__.search

(source)

UPDATE: ActiveAdmin has now a section in his docs about this problem.


When you use ransack,

Shop.search(params[:query])

returns a Ransack::Search object, hence the "undefined method each' for Ransack::Search" error.Change that to

@shops = Shop.search(params[:query], load:true).result