# Discounted total

**URL:** <https://www.ruby-forum.com/t/discounted-total/117843>\
**Category:** Rails\
**Created:** [October 21, 2007, 12:54pm UTC](https://www.ruby-forum.com/t/discounted-total/117843 "2007-10-21T12:54:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [October 21, 2007, 12:54pm UTC](https://www.ruby-forum.com/t/discounted-total/117843/1 "2007-10-21T12:54:25Z")

</div>

Hi again,

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:

def discounted\_total  
subtotal = @cart.total  
discount = @customer.discount / 100  
subtotal = (subtotal \* discount)  
@discounted = @cart.total - subtotal  
end

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):

31:

**$\<%=  
two\_dec(@cart.discounted\_total) %\>**

app/models/cart.rb:38:in `discounted\_total’

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?

Thanks,  
Elle

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [October 21, 2007, 11:38pm UTC](https://www.ruby-forum.com/t/discounted-total/117843/2 "2007-10-21T23:38:05Z")

</div>

I suppose it would be because @cart is not available within cart.rb.  
Try self.total instead? e.g

def discounted\_total  
subtotal = self.total  
discount = self.customer.discount / 100  
subtotal = (subtotal \* discount)  
self.total - subtotal  
end

not sure how your cart class is constructed and its relationship with  
the Customer class so this may not be correct when dealing with  
getting the discount amount.

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [October 22, 2007, 5:47am UTC](https://www.ruby-forum.com/t/discounted-total/117843/3 "2007-10-22T05:47:07Z")

</div>

This works great in my cart views (view\_cart and checkout). Then I  
tried to duplicate it in my order.rb class and it doesn’t work again.  
Most of the time I get an error that I have a nil object.  
My orders table has the following columns: customer\_id, discount,  
total…

The order.rb code is:

def total  
order\_items.inject(0) {|sum, n| n.price \* n.amount + sum}  
end

def discount  
current\_order = Order.find(params[:id])  
discount = current\_order.discount  
end

def discounted\_total  
subtotal = self.total  
discount = self.discount / 100  
subtotal = (subtotal \* discount)  
self.total - subtotal  
end

The total is calculated exactly the same as in the cart. The  
relationships are exactly as the cart but with order\_items:  
has\_many :order\_items  
has\_many :products, :through =\> :order\_items  
belongs\_to :customer

And I tried to use @order.discounted\_total in my views but it doesn’t  
work.  
Why wouldn’t it work here when it worked in mt cart? and how should I  
fix it?

TIA,  
Elle
