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

  • SEARCH
  • Home
  • 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 9136815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:00:13+00:00 2026-06-17T09:00:13+00:00

Picture a normal invoice: On it, you have several items. Each item has a

  • 0

Picture a normal invoice: On it, you have several items. Each item has a quantity and a price per unit, among other things (unit and description).

The total amount for each item is calculated like this: quantity * price per unit.
This is done for each item. Then, the overall invoice’s net amount is the sum of all the totals. Add VAT, and you have the invoice’s gross amount.

This is what I am trying to do with my Rails app. An invoice has many items and accepts nested attributes for them. Generally, this all works fine.

Following the logic, all I need to enter in manually is the price per unit and the quantity for each item as well as the invoice’s VAT. The totals and the resulting net and gross amount should be calculated automatically. I want to achieve this using the before_save filter.

Here is my invoice model:

before_save :calculate_net_amount, :calculate_gross_amount

def calculate_net_amount
  self.items do |item|
    self.net_amount += item.total
  end
end

def calculate_gross_amount
    self.gross_amount = self.net_amount * (1 + self.vat_rate)
end

This is the item model:

before_save :calculate_total

def calculate_total
  self.total = self.quantity * self.price_per_unit
end

And here is my spec that is failing:

it "calculates the net amount from all item totals" do
  invoice = FactoryGirl.build(:invoice)
  item = invoice.items.build(quantity: 2, unit: "Unit", description: "Desc", price_per_unit: 2)
  invoice.save
  invoice.net_amount.should == 4
end

It uses this invoice factory:

FactoryGirl.define do
  factory :invoice do
    association :client
    currency "EUR"
    vat_rate 0.19
    net_amount 1
    payment_on "2013-01-01"
    status "paid"
  end
end

The test basically does the following: An invoice with 2 things that both cost USD 2 should have a net amount of USD 4. Instead, the test returns 1, which seems to come from the factory and apparently isn’t overwritten. If I remove it from the fixture, it says that it cannot calculate the gross amount anymore, since it can’t use * on any nil object.

I am assuming I am doing something wrong with the filters and the order in which they are called – the total amounts are calculated correctly, so it has to be something about the calculate_net_amount method that’s going wrong and as a result it can’t calculate the gross amount anymore.

Can you see what I am doing wrong?

  • 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-06-17T09:00:14+00:00Added an answer on June 17, 2026 at 9:00 am
    self.items do |item|
    

    should be

    self.items.each do |item|
    

    Since items is an accessor, which is a method, it can technically take a block, but that block isn’t getting called, so no summing is happening. It’s an easy typo to make.

    As an aside, it’s better is to sum using inject:

    self.net_amount = self.items.inject(0){|sum, item| sum + item.total}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So i have a picture gallery with eight categories. Each category has 400+ images,
Assume that I have a normal picture box with a picture loaded, how can
Picture the following situation. I have an XML document as follows, <Form> <Control Type=Text
Picture says 1000 words: http://d.pr/ZeBI Basically in most other browsers, when focus on the
the picture below has 2 divs, 1 is the part where the login form
I have this code: (Normal) <div class=thumbInnerContainer> <div class=thumbs> <img src=/uploads/2012/09/image1.jpeg class=thumb_img title=Image Title
I have two xml files. One xml file is a Shape and the other
I have for example the following picture displayed in my gui: http://www.zielpunkt.at/image/produkte/1354097303-airwick.jpg Unfortunately the
I have some HTML and CSS code like below <html> <head> <style type=text/css> img.normal
I want get the user profile large or normal picture from facebook. Now I

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.