0

I am having problems building a new Rails app.

When trying to run @invoice.total in the view, I get this error:

undefined method `*' for nil:NilClass

However, running @invoice.total works in the console. I have tried using HAML and/or ERB with the same problem.

The code which runs for @invoice.total is in the model and is below:

def items_total
  items_total = 0
  self.invoice_items.each do |i|
    items_total += i.price * i.quantity
  end
  items_total
end


# instead of copying this code all of the time
def vat_calc
  (1 + self.vat_rate / 100)
end

def discount_calc
  (1 - self.discount / 100)
end

# total times to add vat on top and remove discount
def total
  items_total * discount_calc * vat_calc
end

The invoice has vat_rate and discount set to 0 and each of the items (there are 3) have inc_vat as 1.

What's the problem?

1
  • Are you using select in a controller action somewhere to limit the fields returned for your invoice items? That can cause the behavior you see where it works fine in a console but fails in the app.
    – x1a4
    Aug 7, 2012 at 22:04

1 Answer 1

0

Turns out I had @invoice_item = @invoice.invoice_items.new in the show area of the invoices controller instead of @invoice_item = InvoiceItem.new for the Invoice Item creation form.

It had been working though :/

Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.