Setting default search parameter on Ransack for rails
Asked Answered
D

4

17

I've been wracking my brain over this but can't get it. I feel like the answer is probably obvious.

What I'm trying to do is the following:

I have an index controller which lists a series of Jobs which I can search using Ransack. Each job has a completion date which either has a date in it or is null (unfinished). Currently, the search itself works great. I would like to make it so that the index page loads up showing only the unfinished work, but I also want it to work so that when someone does run a search, returns results for both finished and unfinished work.

Any help would be greatly appreciated. In the code below, :actual is the name of the field with the completion date. I also was looking around the web and thought that maybe something like the DEFAULT_SEARCH_PARAMETER={} that I have in the Job model might work but I couldn't seem to get it to. Here is the code:

class Job < ActiveRecord::Base
  DEFAULT_SEARCH_PARAMETER ={}
  attr_accessible :items_attributes, :actual
end

def index
   @search = Job.search(params[:q] || Job::DEFAULT_SEARCH_PARAMETER)
   @search.build_condition
   @results = @search.result
   @job = @results.paginate(:per_page => 10, :page => params[:page])
end
Davon answered 8/11, 2012 at 7:8 Comment(0)
R
20

I think you could just apply your own filter when the search parameters don't exist:

def index
  @search = Job.search(params[:q])
  @results = @search.result
  @results = @results.where(:your_date => nil) unless params[:q]
  @job = @results.paginate(:per_page => 10, :page => params[:page])
end
Ralf answered 8/11, 2012 at 19:14 Comment(2)
Thanks! This worked. I would vote this up but I don't have the reputation.Davon
Check ricsrock's better answer before following this one.Clift
G
39

Late to the party, but thought I'd suggest an alternate approach in case someone else comes across this.

The answer above works, but its disadvantage is that the default is not added to Ransack's search object, so - if you are using a search form - the default selection is not shown in the form.

The following approach adds the default to the search object and therefore will appear in your search form.

def index
  @search = Job.search(params[:q])
  @search.status_cont = 'Open' unless params[:q] #or whatever, must use Ransack's predicates here
  @results = @search.result
  @job = @results.paginate(:per_page => 10, :page => params[:page])
end
Griffon answered 18/11, 2013 at 17:15 Comment(5)
Hey @Griffon any ideas on how to do this with a whitelisted scope?Evanescent
Nice answer @ricsrock. Since modifies the Ransack search, its its easy to have the default you've added show up correctly on a search section of the rendered page.Jhelum
Very good! It works perfectly, also with the respective default values in the search form (as Steven has pointed out already).Fled
Came across your answer for the second time. Still great. This should be the right answer! @alexander-moore, please mark this answer as the correct one!Ari
In case you want sorting to preserve the defaults; replace @search.status_cont = 'Open' unless params[:q] with @search.status_cont = 'Open' unless params[:q]&.except(:s)&.present?Wishbone
R
20

I think you could just apply your own filter when the search parameters don't exist:

def index
  @search = Job.search(params[:q])
  @results = @search.result
  @results = @results.where(:your_date => nil) unless params[:q]
  @job = @results.paginate(:per_page => 10, :page => params[:page])
end
Ralf answered 8/11, 2012 at 19:14 Comment(2)
Thanks! This worked. I would vote this up but I don't have the reputation.Davon
Check ricsrock's better answer before following this one.Clift
B
2

Many years later I found myself with this exact problem so I thought I'd chime in with a solution I'm using. Set default search params in the controller and reverse merge them into params[:q]:

def index
  default_search_params = {
    status_cont: "open"
  }

  @search = Job.search((params[:q] || {}).reverse_merge(default_search_params))
  ...
end
Burn answered 25/6, 2021 at 0:42 Comment(0)
N
0

So by default, you want the page to load with records where actual is nil. And later when the user searches you want to go back to how your search was working before.

Give this a try.

def index
   @search = Job.search(params[:q] || Job::DEFAULT_SEARCH_PARAMETER)
   @search.build_condition
   @results = @search.result
   if @results.nil?
        @results=Job.find(:all, :conditions => ["actual = NULL"] )
   end

   @job = @results.paginate(:per_page => 10, :page => params[:page])
end    
Nonanonage answered 8/11, 2012 at 18:57 Comment(1)
I gave this a try but it didn't work. Thanks for the help though!Davon

© 2022 - 2024 — McMap. All rights reserved.