Search code examples
ruby-on-railsactiverecordnamed-scopemeta-searchactive-relation

Chain meta_search results


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 ?


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])