Let's say I have a User table connected to a Payment table. I want to find the sum of all of a user's payments, even if they didn't make any. I might do something like:
User.left_joins(:payments).group(:id).pluck(:id, "SUM(amount)")
This would return nil values for the users who don't have a payment record. However, if I apply any kind of filtering, that aspect is lost. For example:
User.left_joins(:payments).where("payment_date > '2021-01-01'").group(:id).pluck(:id, "SUM(amount)")
The output will only contain users who made a payment after 2021-01-01. There will be no mention of users who only made payments before 2021 or never made a payment at all. Is there a way to maintain this information as nil or 0, similar to how it was done in the first query?