Reputation: 15
I want search created_at between Date.today.beginning_of_day..Date.today.end_of_day
like
Order.search({created_at_between: Date.today.beginning_of_day..Date.today.end_of_day }).result
but is not work
How do i make to search created_at
between dates?
Upvotes: 1
Views: 2320
Reputation: 91
I agree with the accepted answer from Frank except the version of arel I was using did not have 'between' defined. So if you are using an older version, see my quick and dirty solution below.
In my view I have a ransack search field like
= f.text_field :request_date_between, class: 'daterange'
That will send a date to the controller like
'2015-10-01 to 2015-10-31'
then in my down and dirty ransack.rb initializer I have;
Ransack.configure do |config|
config.add_predicate 'between',
arel_predicate: 'between',
formatter: proc { |v| v.split(' to ') },
type: :string
end
module Arel
module Predications
def between other
gteq(other[0]).and(lt(other[1]))
end
end
end
Upvotes: 0
Reputation: 2162
config/initializers/ransack.rb
Ransack.configure do |config|
config.add_predicate 'between',
arel_predicate: 'between',
formatter: proc { |v|
parts = v.split(',')
OpenStruct.new(begin: parts[0], end: parts[1])
},
validator: proc { |v| v.present? },
type: :string
end
Then created_at_between '2014-01-01,2014-01-02'
works!
Upvotes: 3
Reputation: 2786
here is the solution,
params[:q] = {:created_at_gt => Date.current.beginning_of_day, :created_at_lt => Date.current.end_of_day}
Order.search(params[:q]).result
Upvotes: 1