# Linking a class to itself

**URL:** <https://www.ruby-forum.com/t/linking-a-class-to-itself/110355>\
**Category:** Rails\
**Created:** [July 25, 2007, 6:22pm UTC](https://www.ruby-forum.com/t/linking-a-class-to-itself/110355 "2007-07-25T18:22:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 25, 2007, 6:22pm UTC](https://www.ruby-forum.com/t/linking-a-class-to-itself/110355/1 "2007-07-25T18:22:06Z")

</div>

I have a few classes. One of them, articles, can have other articles  
linked to it. Is the best way to model this with a  
belongs\_to\_and\_has\_many, ie

class Article  
belongs\_to\_and\_has\_many :articles  
…  
end

So i can then say (eg in a list view)

for article in @article.articles  
#do something with article  
end

Will this work?

Assuming this is the best way to do it, in the join table, which will be  
called articles\_articles, i’m going to have to rename one or both of the  
columns from the default of article\_id (since i can’t have two fields  
ith the same name). How do i tell rails the new column names?

Appreciate any advice…

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 25, 2007, 7:01pm UTC](https://www.ruby-forum.com/t/linking-a-class-to-itself/110355/2 "2007-07-25T19:01:58Z")

</div>

> Max W. wrote:
> 
> Assuming this is the best way to do it, in the join table, which will be  
> called articles\_articles, i’m going to have to rename one or both of the  
> columns from the default of article\_id (since i can’t have two fields  
> ith the same name). How do i tell rails the new column names?
> 
> Appreciate any advice…

I did something like this for old application (done twice to make it two  
way):

class Company  
has\_and\_belongs\_to\_many :corporate\_shares\_held\_in,  
:class\_name =\> ‘Company’,  
:join\_table =\> ‘company\_share\_holders’,  
:foreign\_key =\> ‘company\_id’,  
:association\_foreign\_key =\> ‘share\_holder\_company\_id’

has\_and\_belongs\_to\_many :corporate\_share\_holders,  
:class\_name =\> ‘Company’,  
:join\_table =\> ‘company\_share\_holders’,  
:foreign\_key =\> ‘share\_holder\_company\_id’,  
:association\_foreign\_key =\> ‘company\_id’  
end

You should be able to use has\_many :through though - try searching  
google “self referential has\_many through”.

Cheers  
Luke

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 25, 2007, 7:32pm UTC](https://www.ruby-forum.com/t/linking-a-class-to-itself/110355/3 "2007-07-25T19:32:15Z")

</div>

> class Company  
> has\_and\_belongs\_to\_many :corporate\_shares\_held\_in,  
> :class\_name =\> ‘Company’,  
> :join\_table =\> ‘company\_share\_holders’,  
> :foreign\_key =\> ‘company\_id’,  
> :association\_foreign\_key =\> ‘share\_holder\_company\_id’

Thanks - i actually just did something very similar and it worked - i  
set up join a table called article\_links, where links are articles as  
well. Then, like you say above:

has\_and\_belongs\_to\_many :links,  
:class\_name =\> “Article”,  
:association\_foreign\_key =\> “link\_id”,  
:join\_table =\> “articles\_links”

Seems to work great, and it’s much nicer talking about “link in  
@article.links” than “article in @article.articles” which would have  
been confusing 🙂

cheers!

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 26, 2007, 12:46am UTC](https://www.ruby-forum.com/t/linking-a-class-to-itself/110355/4 "2007-07-26T00:46:02Z")

</div>

Max, I have the same situation and model it like this:

class Article…  
has\_and\_belongs\_to\_many :related\_articles,  
:class\_name =\> “Article”,  
:join\_table =\> “related\_articles”,  
:association\_foreign\_key =\> “related\_article\_id”,  
:foreign\_key =\> “article\_id”

The related\_articles table just has article\_id and related\_article\_id  
(no id column required)

Regards,

–Kip

On Jul 26, 12:22 am, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
