Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Hopefully i understood the description of the problem correctly but i was thinking here's one way you could lay your database and models out. I also don't think you need the ListIndex:</p> <p>Database Tables</p> <pre><code>Categories: id, title ShoppingLists: id, user_id, order, active Items: id, title ShoppingListItems: id, item_id, shopping_list_id, quantity CategorizedItems: id, category_id, item_id Users: id, name </code></pre> <p>Models</p> <pre><code>User: has_many shopping_lists ShoppingList: belongs_to user has_many shopping_list_items has_many items, through shopping_list_items Items: has_many categorized_items has_many categories, through categorized_items (optional: you could query an item for the shopping lists that it is on) has_many shopping_list_items has_many shopping_lists, through shopping_list_items Categories: has_many categorized_items has_many items, through categorized_items </code></pre> <p>My thinking is this --</p> <p>Individual categories are basically static, that's pretty straight forward.<br> Shopping lists represent a User's (via the user_id) list of items that will be purchased. The link between an item and a shopping list could take place in a join table called ShoppingListItems where each row links together the relationship between a list, an item, and the quantity. </p> <p>Items are interesting because in your example an item is something that can actually be in multiple categories. i.e. "pants" can be in boys/girls/men/women and probably pets :(. To support that I think you can use another join table called CategorizedItems that basically lets you query for "items in a particular category" or "categories an item is in".</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload