Forum: Ruby on Rails Running total of a field in child records

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jong Hian Zin (Guest)
on 2006-03-20 08:30
(Received via mailing list)
What is the best way to get the running total of a number field in child
records. Let say I have an Order model, which has_many :line_items, and
each
LineItem has an amount field. I would like to get the grand total of a
specific Order. Is it a good idea to add a 'total' column in the
'orders'
table and update it every time a line_item is created/updated/deleted?
Emin H. (Guest)
on 2006-03-20 10:11
(Received via mailing list)
http://wiki.rubyonrails.org/rails/pages/MagicFieldNames

see Counter Cache section as one of the solutions
This topic is locked and can not be replied to.