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

落爺英雄遲暮 提交于 2019-12-14 00:50:08

问题


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)


来源:https://stackoverflow.com/questions/6765012/integrating-meta-search-gem-in-index-with-existing-geocoder-gem-search-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!