2

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 ?

Naveed
  • 11,057
  • 2
  • 44
  • 63
Kashif Umair Liaqat
  • 107
  • 1
  • 1
  • 11

2 Answers2

3

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])
Kashif Umair Liaqat
  • 107
  • 1
  • 1
  • 11
2

What about searching in one go

@result = User.search  params[:search].merge(params[:filters])
Naveed
  • 11,057
  • 2
  • 44
  • 63
  • It cannot be done in one go. params[:search] have a key "user_notes_note_name_equals" params[:filters] also have a key "user_notes_Note_name_equals" but both have different values to search. So if we merge them then there will be only one key "user_notes_note_name_equals" with the latest assigned value as both keys are same. The previous value will be overwritten. – Kashif Umair Liaqat Aug 10 '12 at 11:33
  • 1
    Oh man then your search result should be empty and it'll always return empty result.how can one note_name have two name ? as for as i understand meta_search's generated query would be like "SELECT * form users (join here) WHERE user_notes.name = 'blah' AND user_notes.name = 'blah2". are you sure you have two keys with same name is not second key used for orders ? – Naveed Aug 10 '12 at 12:06