title: Database design advice in Rails, The Pizza, Topping and Order
body: <p>Yes, its another database question with the pizza analogy.
  The ones I've seen don't seem to click just right, so here goes.</p>
  
  <p>I need to be able to order multiple pizzas with any number of toppings in a single order.
  Its a basic shopping cart pattern, but the toppings are making it difficult to implement.</p>
  
  <p>The base models are,</p>
  
  <pre><code>class Pizza &lt; ActiveRecord::Base
    has_and_belongs_to_many :orders
    has_and_belongs_to_many :toppings
  end
  
  class Topping &lt; ActiveRecord::Base
    has_and_belongs_to_many :pizzas  
  end
  </code></pre>
  
  <p>Pizza and Topping tables serve as the holder for the information of pizza and topping types that can be ordered.</p>
  
  <pre><code>class OrdersPizzas &lt; ActiveRecord::Base
    belongs_to :order
    belongs_to :pizza
  end
  
  class PizzasToppings &lt; ActiveRecord::Base
    belongs_to :topping
    belongs_to :pizza
  end
  
  class Order &lt; ActiveRecord::Base
    has_and_belongs_to_many :pizzas
  end
  </code></pre>
  
  <p><strong>[Added]</strong></p>
  
  <p>Some clarification, the database design is intended for a shopping app, so being able to setup a "menu" by an admin, and users being able to see the menu, and order from it is imperative.</p>
  
  <p>The operation of this goes in two steps, first you need to make a menu by creating the Pizzas and toppings, and associating them, not all pizzas can have all the toppings.</p>
  
  <blockquote>
    <p>A Cheese Pizza can have extra olives, and extra cheese as toppings</p>
    
    <p>A Peperoni Pizza can have only extra cheese for toppings</p>
  </blockquote>
  
  <p>Step two is actually placing the orders according to the entered data, thus all the many to manys.</p>
  
  <p>Now the question is, after entering the data above, I want to place an order</p>
  
  <blockquote>
    <p>Peperoni Pizza with extra cheese</p>
    
    <p>Cheeze Pizza with extra olives</p>
  </blockquote>
  
  <p>in a single order, how should it be designed?</p>
  
  <p>All I can think of is to add a </p>
  
  <pre><code>class OrdersPizzasToppings &lt; ActiveRecord::Base
    belongs_to :orders_pizzas
    belongs_to :topping
  end
  </code></pre>
  
  <p>so I can distinguish which pizza has the toppings, but that does not look like the right way to go.</p>