Skip to content

Instantly share code, notes, and snippets.

@MichaelCPell
Created June 30, 2016 16:30
Show Gist options
  • Save MichaelCPell/c0ec7df49272aeb5ba0a65e7cdd45e29 to your computer and use it in GitHub Desktop.
Save MichaelCPell/c0ec7df49272aeb5ba0a65e7cdd45e29 to your computer and use it in GitHub Desktop.
if @order_item = @order.order_items.where(product_id: params[:product_id])
@order_item.quantity = @order_item.quantity + 1
@order_item.save
else
# Whatever
end
@jgrant29
Copy link

What do you think?
Got it with:
def create
@order = current_order
if @order_item = @order.order_items.find_by(order_item_product)
@order_item.quantity = @order_item.quantity + @order_item.quantity
@order_item.update_attributes(order_item_quantity)
else
@order_item = @order.order_items.new(order_item_params)
@order.save
session[:order_id] = @order.id
end
end

def order_item_params
params.require(:order_item).permit(:quantity, :product_id, :book_id, :code, :product_ids => [])
end
def order_item_quantity
params.require(:order_item).permit(:quantity)
end
def order_item_product
params.require(:order_item).permit(:product_id)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment