Integrating meta_search gem in index with existing

2019-06-19 11:13发布

问题:

I have already implemented a location based search using geocoder and am having trouble integrating the meta_search gem. I'm trying to integrate meta_search into my object_controller index to allow users to filter and sort search results by an objects :attributes after they have already searched by location.

My object_controller:

def index 
  if params[:search].present?
   @objects = Object.near(params[:search], 50, :order => :distance).paginate(:page => params[:page], :per_page => 9)
  else
   @objects = Object.paginate(:page => params[:page], :per_page => 9)
  end
end

Any idea how best to integrate the @search into the index required by the meta_search gem?

Here is what the meta_search github recommends for the index:

def index
  @search = Article.search(params[:search])
  @articles = @search.all   # load all matching records
  # @articles = @search.relation # Retrieve the relation, to lazy-load in view
  # @articles = @search.paginate(:page => params[:page]) # Who doesn't love will_paginate?
end

Thanks so much,

Will

回答1:

I believe both the geocoder and meta_search query methods return an ActiveRecord::Relation therefore you should be able to chain them:

@objects = Object.near(params[:search], 50, :order => :distance).search(params[:search]).relation.paginate(:page => params[:page], :per_page => 9)

or if you need the search object to be separate:

@search = Object.near(params[:search], 50, :order => :distance).search(params[:search])
@objects = @search.relation.paginate(:page => params[:page], :per_page => 9)