Skip to content

Instantly share code, notes, and snippets.

@lingceng
Last active December 22, 2017 10:45
Show Gist options
  • Save lingceng/65c58512d9bbb50799c7 to your computer and use it in GitHub Desktop.
Save lingceng/65c58512d9bbb50799c7 to your computer and use it in GitHub Desktop.
Custom ransacker predicates to query date range
<%= search_form_for @q, url: production_status_changes_path, class: 'form-inline' do |f| %>
<%= f.label 'Create At' %>
<%= f.search_field :created_at_end_of_day_lteq, class: 'form-control input-sm', 'datepicker' => true %>
<% end %>
# config/initializers/ransack.rb
#
# Custom ransack predicate to simplify query for date range
#
# See lib/ransack.rb in ransack gem
# See wiki https://github.com/activerecord-hackery/ransack/wiki/Custom-Predicates
Ransack.configure do |config|
config.add_predicate 'end_of_day_lteq',
arel_predicate: 'lteq',
formatter: proc { |v| v.end_of_day },
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment