I have the following class method in my Product model that calculates total price within the basket:
def self.total_basket_price(basket)
where(id: basket.to_a).sum(:price)
end
And i have this within my view:
<%= number_to_currency(Product.total_basket_price(basket)) %>
Both work as expected until i try to implement a purchase method in my Order model:
def purchase
response = GATEWAY.purchase(Product.total_basket_price(basket), credit_card, purchase_options)
end
It throws out an undefined local variable or method
on the (basket)
above.
I can't see why the basket
is undefined.
You are seeing this error, well, because there is no method or local variable basket
in your model. So, either:
a) Define a method that returns the basket object:
def basket
# some logic here to return basket.
end
b) Pass in the basket to your method:
def purchase(basket)
response = GATEWAY.purchase(Product.total_basket_price(basket), credit_card, purchase_options)
end
c) Or, if you are working within an instance of Basket
, pass in the instance as self
instead of basket
.
response = GATEWAY.purchase(Product.total_basket_price(self), credit_card, purchase_options)