# Has\_many

**URL:** <https://www.ruby-forum.com/t/has-many/109990>\
**Category:** Rails\
**Created:** [July 20, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/has-many/109990 "2007-07-20T23:37:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pardoengreg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a3d4f5/32.png) [@pardoengreg](https://www.ruby-forum.com/u/pardoengreg)\
**Post date:** [July 20, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/has-many/109990/1 "2007-07-20T23:37:17Z")

</div>

Hi,

I have a table collection and a table collectionorganisation. The  
collectionorganisation table is used to define a hierarchy between  
collections.  
So, in the table collectionorganisation there is two foreign key :  
parent\_id and son\_id.

In the collection model, I define the two following has\_many with  
dependent =\> destroy :

has\_many :collectionorganisation, :foreign\_key =\> “parent\_id”,  
:dependent =\> :destroy  
has\_many :collectionorganisation, :foreign\_key =\> “son\_id”, :dependent  
=\> :destroy

Unfortunatly, only the last depedent(son\_id) is took into account. So  
when a delete a collection that is present in the field son\_id of a  
record of collectionorganisation, then this record (in  
collectionorganisation) is correctly deleted. But, if the collection is  
present in the field parent\_id, then nothing is done.

Someone can help me ?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![pardoengreg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a3d4f5/32.png) [@pardoengreg](https://www.ruby-forum.com/u/pardoengreg)\
**Post date:** [July 21, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/has-many/109990/2 "2007-07-21T17:54:16Z")

</div>

GrÃ©gory Pardoen wrote:

> I have a table collection and a table collectionorganisation. The  
> collectionorganisation table is used to define a hierarchy between  
> collections.  
> So, in the table collectionorganisation there is two foreign key :  
> parent\_id and son\_id.
> 
> In the collection model, I define the two following has\_many with  
> dependent =\> destroy :
> 
> has\_many :collectionorganisation, :foreign\_key =\> “parent\_id”,  
> :dependent =\> :destroy  
> has\_many :collectionorganisation, :foreign\_key =\> “son\_id”, :dependent  
> =\> :destroy
> 
> Unfortunatly, only the last depedent(son\_id) is took into account. So  
> when a delete a collection that is present in the field son\_id of a  
> record of collectionorganisation, then this record (in  
> collectionorganisation) is correctly deleted. But, if the collection is  
> present in the field parent\_id, then nothing is done.
> 
> Someone can help me ?

[http://blog.hasmanythrough.com/2006/4/21/self-referential-through](http://blog.hasmanythrough.com/2006/4/21/self-referential-through)

For best results, you’ll want to name your tables according to Rails’  
conventions.

For the Collection model, the table should be `collections`. If the join  
model is CollectionOrganization, the table should be  
`collection_organizations`.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![pardoengreg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a3d4f5/32.png) [@pardoengreg](https://www.ruby-forum.com/u/pardoengreg)\
**Post date:** [July 21, 2007, 9:40pm UTC](https://www.ruby-forum.com/t/has-many/109990/3 "2007-07-21T21:40:27Z")

</div>

Josh S. wrote:

> GrÃ©gory Pardoen wrote:
> 
> > I have a table collection and a table collectionorganisation. The  
> > collectionorganisation table is used to define a hierarchy between  
> > collections.  
> > So, in the table collectionorganisation there is two foreign key :  
> > parent\_id and son\_id.
> > 
> > In the collection model, I define the two following has\_many with  
> > dependent =\> destroy :
> > 
> > has\_many :collectionorganisation, :foreign\_key =\> “parent\_id”,  
> > :dependent =\> :destroy  
> > has\_many :collectionorganisation, :foreign\_key =\> “son\_id”, :dependent  
> > =\> :destroy
> > 
> > Unfortunatly, only the last depedent(son\_id) is took into account. So  
> > when a delete a collection that is present in the field son\_id of a  
> > record of collectionorganisation, then this record (in  
> > collectionorganisation) is correctly deleted. But, if the collection is  
> > present in the field parent\_id, then nothing is done.
> > 
> > Someone can help me ?
> 
> [has\_many :through - Self-referential has\_many :through associations](http://blog.hasmanythrough.com/2006/4/21/self-referential-through)
> 
> For best results, you’ll want to name your tables according to Rails’  
> conventions.
> 
> For the Collection model, the table should be `collections`. If the join  
> model is CollectionOrganization, the table should be  
> `collection_organizations`.
> 
> –  
> Josh S.  
> [http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

Thanks for those informations.

I didn’t think about :through .  
And I agree for the use of ror’s convention.

I will try that.

Best regards,

Greg
