Search⌘ K
AI Features

Decrementing Product Quantity

Explore how to update your Rails API to decrement product quantities as orders are placed. This lesson guides you through adding quantity attributes, handling validations, adjusting order creation to accept product quantities, and managing stock updates via the Placement model. You'll learn to maintain accurate inventory and secure order processing in your API.

This chapter will take care of some validations on the Order model that make sure it is placeable. We will think about the following concerns:

  • How to decrement the current product quantity when an order is placed.
  • What happens when products are not available?

We’ll probably need to update the JSON output for the orders a little, but we can save that for now. Let’s dive in and get coding.

Add quantity attribute to products

Define and run migration

We will update the product quantity on this first stop to ensure every order will deliver the actual product. Currently, the Product model doesn’t have a quantity ...