Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 540
Will Dennis Avatar asked Jul 20 '11 16:07

Will Dennis


1 Answers

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)
like image 65
Carlos Ramirez III Avatar answered Oct 03 '22 08:10

Carlos Ramirez III