# Has\_many relationship question

**URL:** <https://www.ruby-forum.com/t/has-many-relationship-question/113369>\
**Category:** Rails\
**Created:** [August 30, 2007, 12:34pm UTC](https://www.ruby-forum.com/t/has-many-relationship-question/113369 "2007-08-30T12:34:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mariko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@mariko](https://www.ruby-forum.com/u/mariko)\
**Post date:** [August 30, 2007, 12:34pm UTC](https://www.ruby-forum.com/t/has-many-relationship-question/113369/1 "2007-08-30T12:34:42Z")

</div>

Say I have a model called Order. Can I specify that an order “:has\_many  
orders” ?

That is, an order has many other orders associated with it.

I’m trying to do this, but I’m getting an error saying “undefined method  
order.” Is there a join table that I need to create?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![mariko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@mariko](https://www.ruby-forum.com/u/mariko)\
**Post date:** [August 30, 2007, 1:11pm UTC](https://www.ruby-forum.com/t/has-many-relationship-question/113369/2 "2007-08-30T13:11:57Z")

</div>

Mariko C. wrote:

> Say I have a model called Order. Can I specify that an order “:has\_many  
> orders” ?
> 
> That is, an order has many other orders associated with it.
> 
> I’m trying to do this, but I’m getting an error saying “undefined method  
> order.” Is there a join table that I need to create?
> 
> Thanks in advance.

Hi Mariko C.  
If you are writing code in Order model then  
:has\_many orders, seems to be an erronotical logic.  
if you have more then one item in a single order then u have to write  
has\_many ordered\_items  
(here ordered\_items is a table which linked with orders table and having  
fields like id, order\_id, item\_id, etc.)  
B’coz logically an Order doesn’t have many orders, it should be  
order\_items or so.  
Try this, hope you will find it helpful.

Naresh.

---

<div class="post-metadata">

**Author:** ![mariko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@mariko](https://www.ruby-forum.com/u/mariko)\
**Post date:** [August 31, 2007, 7:49am UTC](https://www.ruby-forum.com/t/has-many-relationship-question/113369/3 "2007-08-31T07:49:27Z")

</div>

Thank you both of you for your replies. I got it working in no time.

Mariko

---

<div class="post-metadata">

**Author:** ![mariko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@mariko](https://www.ruby-forum.com/u/mariko)\
**Post date:** [August 30, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/has-many-relationship-question/113369/4 "2007-08-30T13:15:53Z")

</div>

Mariko C. wrote:

> Say I have a model called Order. Can I specify that an order “:has\_many  
> orders” ?
> 
> That is, an order has many other orders associated with it.
> 
> I’m trying to do this, but I’m getting an error saying “undefined method  
> order.” Is there a join table that I need to create?
> 
> Thanks in advance.

This is called a self-referential many-to-many relationship. Example,  
using your ‘orders’, where some orders could be considered suborders of  
another order.

Class Order \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :suborders,  
:class\_name =\> “Order”,  
:join\_table =\> “suborders\_orders”,  
:association\_foreign\_key =\> “suborder\_id”,  
:foreign\_key =\> “order\_id”  
end

then you can

order1main = Order.new(…)  
order1suborder = Order.new(…)  
order1main.suborders \<\< order1suborder
