Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Hello I've been using the Elastic Search in my app using the Railcasts#306 (http://railscasts.com/episodes/306-elasticsearch-part-1) as an example.

Everything was working fine until I installed the Active Admin gem. When I pass in a query in my shops#index page I get a following error:

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

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

Here are the code fragments related to searching:

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

shops/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!

like image 551
Alexander Avatar asked Nov 22 '25 00:11

Alexander


1 Answers

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.

like image 78
Timo Schilling Avatar answered Nov 23 '25 14:11

Timo Schilling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!