I need the "nps" field value from the latest Feedback record for each account based on the created_at date which is a datetime field. I have worked on this, but it's pulling back the first record for each account. Am I going obviously wrong somewhere?
nps_total = 0
nps_records = Feedback.select([:nps, 'MAX(created_at)']).group(:account_id).each do |record|
nps_total += record.nps
end