I am trying to apply some very basic logic to my view but am so far failing.
What I would like to do is return item.discount_price if there are the following conditions:
- The item.product_id is equal to 1
- The Cart.item.quantity count is equal or higher than 2.
Currently I have the following:
Items Model
class Item < ActiveRecord::Base
belongs_to :product
belongs_to :cart
def price_check
if Item.product_id = 1 && Item.quantity.count >= 2
return Item.discount_price
else
return Item.unit_price
end
end
end
View
<% for item in @cart.items %>
<tr class="<%= cycle :odd, :even %>">
...
<td class="price"><%= gbp(item.price_check) %></td>
...
</tr>
The associations are as follows:
Cart - has_many :items
Items - Belongs_to :cart and :products
Products - has_ many :items
The error I keep receiving:
NoMethodError in Carts#show
Showing C:/Sites/checkout/app/views/carts/show.html.erb where line #12 raised:
undefined method `quantity' for #<Class:0x50c3058>
Extracted source (around line #12):
9: <tr class="<%= cycle :odd, :even %>">
10: <td><%=h item.product.name %></td>
11: <td class="qty"><%= item.quantity %></td>
12: <td class="price"><%= gbp(item.price_check) %></td>
13: <td class="price"><%= gbp(item.full_price) %></td>
14: <td><%= button_to 'Remove', item, :method => :delete %></td>
15: </tr>
app/models/item.rb:12:in `price_check'
app/views/carts/show.html.erb:12:in `block in _app_views_carts_show_html_erb___389237738_48997308'
app/views/carts/show.html.erb:8:in `_app_views_carts_show_html_erb___389237738_48997308'
Any help people can offer to fix this would be much appreciated! Thanks E
RadBrad had the right idea, but the wrong implementation