You can do it:
scope :activeDate, -> { where("? BETWEEN startDate AND endDate", Time.now.to_date)}
Since you use a greater or equal value AND less than or equal, you can use the equivalent SQL function "BETWEEN".
You do not need to convert time to date, you can directly call Date.today:
scope :activeDate, -> { where("? BETWEEN startDate AND endDate", Date.today)}
You can improve your scope by following these steps:
scope :activeAtDate, lambda{ |date = Date.today| where("? BETWEEN startDate AND endDate", date) }
And use it as follows:
Product.activeAtDate()
MrYoshiji
source share