Home Return calculated value with rails query
Reply: 0

Return calculated value with rails query

user5051 Published in September 20, 2018, 12:44 pm

I have a query:

@clients = Client.where(location_id: location).includes(:check_ins, :payments, :purchases).where("check_ins.created_at > ?", start_date).references(:check_ins)

that returns an ActiveRecord::Relation array of clients. Each client has_many :payments and each payment has an :amount.

For each client in the ActiveRecord::Relation, I'd like to include the sum of their payment amounts client.payments.sum(:amount). How can I get that returned in the array?

share|improve this question
  • 1
    why do you want to include in each client object - instead you can calculate it from views right? – Narasimha Reddy Feb 2 at 13:06

1 Answer 1

active oldest votes
up vote 1 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.369172 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO