Question

How can I chain metasearchs' search method ?

@result = User.search(params[:search]).search(params[:filters])

We can call chain method on ActiveRecord like

User.active.male.where( age: 14..20)

its chaining is possible on meta search's result ?

Was it helpful?

Solution

I got it working.. If you had the same problem, you can try following.

The search method of meta_search returns MetaSearch::Search::ModelName where ModelName is the name of your model. Meta_search provides a method relation for this object. You can call relation method to get an ActiveRecord::Relation object and then you can call the search method again on that object. See the code below to see what I am exactly talking about.

@result = User.search(params[:search])
@search = @result.relation.search(params[:filters])

Here @result is the instance of MetaSearch::Search::User so we can call relation method to get an instance of ActiveRecord::Relation i.e

@result.relation

and then we can call search method again on this instance. i.e

@result.relation.search(params[:filters])

OTHER TIPS

What about searching in one go

@result = User.search  params[:search].merge(params[:filters])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top