Search code examples
ruby-on-railsruby-on-rails-4ransack

Rails - Saving Ransack Query for later use


I have successfully installed the Ransack gem and have it working in my app for both simple and advanced searches. I am trying to add a feature for users to "Save a Search". Essentially the idea is they can bookmark a search query, and access it later at any time.

I set it up by creating a SavedSearch model. When a user saves the search it takes the params[:q] from their ransack search and stores it in the database.

Later I pull this record from the database, and access the query. I try to recreate the search by passing the query to ransack, but it does not seem to be working. The search pulls ALL RECORDS for that model, rather than simply those that fit the query. Any idea why? I am thinking perhaps the search parameters need to be modified to a different format before passing them back to ransack? Here is my code:

Here is the code from my controller model (advanced_search action works flawlessly, but saved_search does not filter correctly when recycling the query):

  def advanced_search
    @query = params[:q]
    @saved_search = SavedSearch.new
    @q = Variant.search(params[:q])
    @q.build_grouping unless @q.groupings.any?
    @variants  = params[:distinct].to_i.zero? ?
      @q.result.paginate(per_page: 100, page: params[:page]) :
      @q.result.includes(:supplier).paginate(per_page: 100, page: params[:page])

    respond_with @variants
  end

  def saved_search
    @id = params[:id]
    @saved_search = SavedSearch.find(@id)
    @query = @saved_search.query_parameters
    @distinct = @saved_search.distinct

    @q = Variant.search(@saved_search.query_parameters)
    @q.build_grouping unless @q.groupings.any?
    @variants  = @distinct.to_i.zero? ?
      @q.result.paginate(per_page: 100, page: params[:page]) :
      @q.result.includes(:supplier).paginate(per_page: 100, page: params[:page])

    render 'advanced_search'
  end

Here is how the search parameters from a successful query look when they are stored in the database:

{"g"=>{"0"=>{"m"=>"and", "c"=>{"0"=>{"a"=>{"0"=>{"name"=>"total_revenue"}}, "p"=>"gt", "v"=>{"0"=>{"value"=>"1000"}}}}}}, "s"=>{"0"=>{"name"=>"stock_health", "dir"=>"asc"}}}

Solution

  • I think in the database you are storing the query as String.While you are pulling it from database @saved_search.query_parameters is returning you the string.But the ransack needs it to be a Hash.This is the main problem I guess.

    While you are passing through params its gets a Hash but from database its getting String.You need to parse the string you are getting from the @query variable and then pass it to the search.

    For String to Hash conversion you can have a look at How do I convert a String object into a Hash object? question's answers.