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

Hello I have used Elastic Search in my application using rails # 306 ( http://railscasts.com/episodes/306-elasticsearch-part-1 ) as an example .

Everything worked fine until I set the Active Admin attribute. When I submit a request to my stores # index page, I get the following error:

undefined `each 'method for Ransack :: Search>: Ransack :: Search

Any idea why it is using Ransack gem (and not Elastic Search) for searching?

Here are the code snippets related to search:

Shop.rb:

class Shop < ActiveRecord::Base

  include Tire::Model::Search
  include Tire::Model::Callbacks

  (...)

  end

      

shops_controller.rb

  def index
    if params[:query].present?
      @shops = Shop.search(params[:query], load:true)
    else
      @shops = Shop.all
    end
    respond_with(@shops)
  end

      

stores / index.html.erb

<%=form_tag shops_path, :method =>:get do %>
<p><%= text_field_tag :query, params[:query] %>
<%= submit_tag "Search", :name => nil, class: "button small success" %></p>
<% end %>

      

Any help would be appreciated, thanks!

+3


source to share


2 answers


When you use ransack,

Shop.search(params[:query])

      



returns an object Ransack::Search

, hence the error is "undefined method each' for Ransack::Search

". Change this to

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

      

+1


source


Your problem is that you are using ElasticSearch and Ransack (ActiveAdmin dependent) in the same model.

Ransack has protection for this case. You can search in Ransack with search

and ransack

if the model already has a method search

that Ransack does not write.

You need to make sure ElasticSearch is loaded before Ransack (ActiveAdmin) which should solve your problem and you can use your code as before adding ActiveAdmin.

The problem with @nicooga's solution is that it uses Ransack for search and not ElasticSearch.

EDIT: This should be the best solution:

If you are using a bus:



Shop.tire.search

      

( Source )

If you are using elasticsearch-ruby or elasticsearch-rails:

Shop.__elasticsearch__.search

      

( source )

UPDATE: ActiveAdmin now has a section in the docs about this issue.

+5


source







All Articles