# Can a class have the association has\_itself?

**URL:** <https://www.ruby-forum.com/t/can-a-class-have-the-association-has-itself/129032>\
**Category:** Rails\
**Created:** [February 13, 2008, 5:52pm UTC](https://www.ruby-forum.com/t/can-a-class-have-the-association-has-itself/129032 "2008-02-13T17:52:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonas](https://www.ruby-forum.com/u/jonas)\
**Post date:** [February 13, 2008, 5:52pm UTC](https://www.ruby-forum.com/t/can-a-class-have-the-association-has-itself/129032/1 "2008-02-13T17:52:20Z")

</div>

is it possible to implement a recursive foreign key association like  
that?

i’m writing a vocab app, where i want the vocab table to have an id  
and two foreign keys, word1 and word2.  
the word table will have an id, a word-string, and a language

i’m trying to do this as follows:

class Vocab \< ActiveRecord::Base  
belongs\_to :word  
belongs\_to :word

class Word \< ActiveRecord::Base  
has\_many :vocabs  
has\_many :words, :through =\> :vocabs

but it doesn’t work. I’m really new to rails and ruby.

---

<div class="post-metadata">

**Author:** ![jonas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonas](https://www.ruby-forum.com/u/jonas)\
**Post date:** [February 13, 2008, 6:40pm UTC](https://www.ruby-forum.com/t/can-a-class-have-the-association-has-itself/129032/2 "2008-02-13T18:40:38Z")

</div>

not that way 🙂

Vocab must have two word fields (eg the default word\_id and word\_two\_id)

class Vocab …  
belongs\_to :word  
belongs\_to :word\_two, :foreign\_key =\> :word\_two\_id

class Word …  
has\_many :vocabs # referencing the first belongs\_to on word\_id  
has\_many :vocab\_twos, :foreign\_key =\> :word\_two\_id # only if you need  
it…

has\_many :words, :through =\> :vocabs # or :vocab\_twos

not 100% sure that’s exact, but something on that line…
