I need help with a cart object which has_many line_items. If a line_item in the cart is created and it has the same exact attributes as a line_item which is already in the cart I just want to update the pre existing line_items quantity rather than create duplicate objects with separate quantities.
I wrote a few methods in my models to try and make this work but it isn't working. Below is my code:
class LineItem < ActiveRecord::Base
attr_accessible :cart_id, :product_id, :quantity, :unit_price, :product, :cart, :color_id, :size_id, :extra_id
belongs_to :cart
belongs_to :product
belongs_to :color
belongs_to :size
belongs_to :extra
validates :quantity, :presence => true
def update_quantity(qty)
quantity += qty
quantity.save
end
def exists_in_collect?(items)
if items.include?(product)
if color == items.color && size == items.sizes && extra == items.extra
return true
end
else
return false
end
end
end
class Cart < ActiveRecord::Base
attr_accessible :purchased_at
has_many :line_items
has_one :order
def where_line_item_with(prod_id)
line_items.where(:product_id => prod_id)
end
end
class LineItemsController < ApplicationController
def new
@line_item = LineItem.new
end
def create
@line_item = LineItem.new(params[:line_item].merge(:cart => current_cart))
if @line_item.exists_in_collect?(current_cart.line_items)
current_cart.where_line_item_with(product.id).update_quantity(@line_item.quantity)
@line_item.destroy!
else
@line_item.save!
@line_item.update_attributes!(:unit_price => @line_item.item_price)
end
redirect_to current_cart_url
end
def update
@line_item = LineItem.find(params[:id])
@line_item.update_attributes(params[:line_item])
redirect_to current_cart_url
end
Any insight is fully appreciated.
1.You should change your where_line_item_with(prod_id)
to the following:
def where_line_item_with(prod_id)
line_items.where(:product_id => prod_id).first
end
As the where
returns an array and you cannot do update_quantity(@line_item.quantity)
on an array.
2.In exists_in_collect?(items)
- Here your aim is to find if the items of the cart include the item similar to new item. it should be updated as following:
def exists_in_collect?(items)
items.each do |item|
if color == item.color && size == item.sizes && extra == item.extra && product == item.product
return true
end
end
return false
end