A
A
Alan Ikaev2018-12-16 00:22:41
Ruby on Rails
Alan Ikaev, 2018-12-16 00:22:41

How to filter data by past date?

I need to select records by the past date and by the future one, I understand the logic of work, but how can I do this using Ransack?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alfss, 2018-12-16
@alfss

An example straight from the documentation

class Employee < ActiveRecord::Base
  scope :activated, ->(boolean = true) { where(active: boolean) }
  scope :salary_gt, ->(amount) { where('salary > ?', amount) }

  # Scopes are just syntactical sugar for class methods, which may also be used:

  def self.hired_since(date)
    where('start_date >= ?', date)
  end

  def self.ransackable_scopes(auth_object = nil)
    if auth_object.try(:admin?)
      # allow admin users access to all three methods
      %i(activated hired_since salary_gt)
    else
      # allow other users to search on `activated` and `hired_since` only
      %i(activated hired_since)
    end
  end

  private_class_method :ransackable_scopes
end

Employee.ransack({ activated: true, hired_since: '2013-01-01' })

Employee.ransack({ salary_gt: 100_000 }, { auth_object: current_user })

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question