Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3997366
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:23:35+00:00 2026-05-20T07:23:35+00:00

I have an invoice model, that has many invoice items. I have a form

  • 0

I have an invoice model, that “has many” invoice items. I have a form that allows you to create/edit an invoice, and this form accepts nested attributes for the invoice items.

In my invoice model, there is a “total_amount” field that is a calculated field (the sum of “amount” from the invoice items).

I want to calculate this amount when an invoice is created, or updated. I am trying to do this in the before_save event of my invoice model. The code below almost works, however the total that gets saved is always one step behind. I.e. if I have an invoice with a total of $20, and I edit this invoice and change the invoice items to total $15, then save my invoice, the total doesn’t change. If I open the same invoice, and then save it again, the total is updated properly.

I assume that my line below that calculates the sum is accessing the line items that are saved in the database already, and not those that have just been changed and are about to be saved. I don’t however know how to access those.

class Invoice < ActiveRecord::Base
  has_many :invoice_items, :dependent => :destroy

  accepts_nested_attributes_for :invoice_items, :allow_destroy => true 

  before_save :record_total_amount

  private
    def record_total_amount
      self.total_amount = self.invoice_items.sum('amount')
    end
end

Any help would be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:23:35+00:00Added an answer on May 20, 2026 at 7:23 am

    I solved this problem, I had to replace the calculation line with this one:

    self.total_amount = invoice_items.map(&:amount).sum
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following that will enumerate over the model and add an invoice
I have to read invoice ascii files that are structured in a really convoluted
I have an invoicing system that manages debits and credits. Basically the invoice amount
Suppose I have a customer object that has all of the standard customer properties
I've got a Supplier Invoice (SupplierInvoice) parent model that holds a number of orders
Some background: In Germany (at least) invoice numbers have to follow certain rules: The
I have tree tables, Customer, Invoice and InvoiceRow with the standard relations. These I
My site is going to have a credit system that basically works a lot
My problem is hydrating a Viewmodel from a Linq2Sql object that has been returned
I have a class with this method: def telecom_info Rails.cache.fetch("telecom_info_for_#{ref_num}", :expires_in=> 3.hours) do info

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.