简体   繁体   中英

Integrating meta_search gem in index with existing geocoder gem search (rails)

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM