Skip to content

Instantly share code, notes, and snippets.

@alansparrow
Last active December 27, 2015 09:59
Show Gist options
  • Save alansparrow/7308331 to your computer and use it in GitHub Desktop.
Save alansparrow/7308331 to your computer and use it in GitHub Desktop.
app/models/cart.rb
class Cart < ActiveRecord::Base
has_many :line_items, dependent: :destroy
def add_product(product_id)
current_item = line_items.find_by(product_id: product_id)
if current_item
current_item.quantity += 1
else
current_item = line_items.build(product_id: product_id)
end
current_item
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment