# Help with many-to-many relationships

**URL:** <https://www.ruby-forum.com/t/help-with-many-to-many-relationships/125948>\
**Category:** Rails\
**Created:** [January 14, 2008, 3:29am UTC](https://www.ruby-forum.com/t/help-with-many-to-many-relationships/125948 "2008-01-14T03:29:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![weloki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a587f6/32.png) [@weloki](https://www.ruby-forum.com/u/weloki)\
**Post date:** [January 14, 2008, 3:29am UTC](https://www.ruby-forum.com/t/help-with-many-to-many-relationships/125948/1 "2008-01-14T03:29:50Z")

</div>

Suppose there is a set of items, a set of stores, and a set of quantity  
values. Each item is located in at least one store and each store has at  
least one item. For each store, each item has a particular quantity. The  
quantity associated with each item depends upon the store.  
For example, there is an apple and an orange (the items). The possible  
quantities are 10 and 20. The stores are Store A and Store B. An  
acceptable state of affairs according to this is: Store A contains 10  
apples and 10 oranges. Store B contains 10 apples and 20 oranges.  
How should my database be structured to allow users to assign items to  
stores AND set their quantities based on their store location?

I have 3 models:

class Item \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :quantities  
has\_and\_belongs\_to\_many :stores  
end

class Store \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :items  
end

class Quantity \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :items  
end

I have the following tables:  
items, quantities, stores, items\_quantities, and items\_stores.  
The last two are cross-reference tables, named according to convention.

Given what I have so far, a user can associate items to stores and  
quantities to items, but users cannot associate quantities to items  
according to the store the items belong to. So going along with the  
example above, a user can place the apple item in Store A and assign the  
quantity 10 to apples, but there is nothing that specifies that Store A  
contains 10 apples.  
Any ideas?

---

<div class="post-metadata">

**Author:** ![weloki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a587f6/32.png) [@weloki](https://www.ruby-forum.com/u/weloki)\
**Post date:** [January 14, 2008, 3:33am UTC](https://www.ruby-forum.com/t/help-with-many-to-many-relationships/125948/2 "2008-01-14T03:33:39Z")

</div>

Use a has\_many :items, :through =\> “inventory”

and store item\_id, store\_id and quantity in that table.

---

<div class="post-metadata">

**Author:** ![weloki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a587f6/32.png) [@weloki](https://www.ruby-forum.com/u/weloki)\
**Post date:** [January 14, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/help-with-many-to-many-relationships/125948/3 "2008-01-14T16:05:11Z")

</div>

WooHoo! You are a rockstar Ryan.

Ryan B. wrote:

> Use a has\_many :items, :through =\> “inventory”
> 
> and store item\_id, store\_id and quantity in that table.
