We have a referral program. When a referred user subscribes, the referrer should get a percentage of the amount due on the first invoice.
referral_ids = (an array of a bunch of user_ids)
I can get a single invoice per referred user like this:
invoices = Invoice.where(user_id: referred_user_ids, renewal: false).group(:user_id).pluck(:amount_due)
However, because the amount_due
may vary from invoice to invoice, I'd like to know how I can ensure that the record returned from the GROUP BY
is the first /oldest record within that group. Thanks in advance.