Every customer has a discount level assigned to them. when they add
items to the cart and on checkout (or when admin views all orders), I
would like to show the total after discount.
In the orders table, I insert the discount level at the time of order.
I ended up calculating the discounted total in about 3 or 4 of my
views and I thought that there should be a better way of doing this.
My logic tells me that the calculation should be done within the cart
class, where also the total is calculated (maybe my logic is wrong).
So, I added this to my cart.rb class:
subtotal = @cart.total
discount = @customer.discount / 100
subtotal = (subtotal * discount)
@discounted = @cart.total - subtotal
But now I get this error:
NoMethodError in Cart#view_cart
Showing app/views/cart/view_cart.rhtml where line #31 raised:
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.total
Extracted source (around line #31):
So, my question is:
How should I accomplish this? Any ideas how my code could be better?
And how do I get the order.rb see the same discounted total?