I have a situation where I have two models, where there is a 1:1 association between them and some method delegation:
class Sale < ApplicationRecord
has_one :receipt
delegate :date, to: :receipt, prefix: true
end
class Receipt < ApplicationRecord
belongs_to :sale
end
so that you can get the date of a receipt by calling sale_object.receipt_date. What I need is a filter in ActiveAdmin on the Sale index page where I can select all those sales objects where the associated receipt date is in a given range. So I've added this to the AA filters:
filter :receipt_date, as: :date_range
This provides a filter which accepts a start and end date, and it works but only if the days given are separated by at least one day. If I want to filter for all Sale objects on a particular day (say by entering in 1-22-2024 for both the "From" and "To" fields) the query looks for sales where the receipt date is both greater than and less than the given date, and so nothing is returned. I've added a couple of ransackers to the Sale class:
def self.receipt_date_gteq(date)
joins(:receipt).where('receipts.date >= ?', date.to_date)
end
def self.receipt_date_lteq(date)
joins(:receipt).where('receipts.date <= ?', date.to_date + 1.day)
end
with the second date filter adding a day to the query thus making it effectively the end of that day instead of the beginning. This solves the problem with single day filters not working, but for some reason the "From" and "To" fields in the filter don't populate with the entered values. The query values show up in the URL and also in the "Search Status" box below the filters but they are gone from the filter input itself. In this organization it is very common for people to make several progressive filters, starting with a given date range and then drilling down from that. If the values don't populate it will make their job harder and make me a lousy coder. Am I making this more difficult than it needs to be? How can I rework these filters so that same day filtering works, and the values properly populate in the From/To fields?