# Self Reference, Almost There (I Hope!)

**URL:** <https://www.ruby-forum.com/t/self-reference-almost-there-i-hope/190450>\
**Category:** Rails\
**Created:** [June 20, 2010, 8:12pm UTC](https://www.ruby-forum.com/t/self-reference-almost-there-i-hope/190450 "2010-06-20T20:12:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [June 20, 2010, 8:12pm UTC](https://www.ruby-forum.com/t/self-reference-almost-there-i-hope/190450/1 "2010-06-20T20:12:33Z")

</div>

I’m still struggling with the self reference. I _THINK_ I’m getting  
close. I have the following tables:

class CreateCategories \< ActiveRecord::Migration  
def self.up  
create\_table :categories do |t|  
t.string :name, :limit =\> 40  
t.timestamps  
end  
end  
end

class CreateRelationships \< ActiveRecord::Migration  
def self.up  
create\_table :relationships, :id =\> false do |t|  
t.integer :category\_id  
t.integer :parent\_id  
t.integer :sort\_order  
t.timestamps  
end  
end  
end

My models look like this:

class Relationship \< ActiveRecord::Base  
belongs\_to :category, :class\_name =\> ‘Category’,  
:foreign\_key =\> ‘category\_id’  
belongs\_to :parent, :class\_name =\> ‘Category’,  
:foreign\_key =\> 'parent\_id  
end

class Category \< ActiveRecord::Base  
has\_many :relationships, :foreign\_key =\> ‘category\_id’

has\_many :parents, :class\_name =\> ‘Category’,  
:through =\> ‘relationships’,  
:source =\> :parent

has\_many :children, :class\_name =\> ‘Category’,  
:through =\> ‘relationships’,  
:source =\> :category  
end

I can create a category. When I try to add a parent to it (e.g.  
c.parents \<\< p) I get the error

ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the  
association “relationships” in model Category

I’m sure that I’m missing something simple, but I don’t see it. Can  
anyone let me know what I’m missing?

Thanks much  
—Michael

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [June 20, 2010, 8:20pm UTC](https://www.ruby-forum.com/t/self-reference-almost-there-i-hope/190450/2 "2010-06-20T20:20:38Z")

</div>

On 20 June 2010 19:12, Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class CreateRelationships \< ActiveRecord::Migration  
> def self.up  
> create\_table :relationships, :id =\> false do |t|  
> t.integer :category\_id  
> t.integer :parent\_id  
> t.integer :sort\_order  
> t.timestamps  
> end  
> end  
> end

If “relationships” is a join table, then it can’t have any fields  
other than the two foreign keys.

> class Relationship \< ActiveRecord::Base  
> belongs\_to :category, :class\_name =\> ‘Category’,  
> :foreign\_key =\> ‘category\_id’  
> belongs\_to :parent, :class\_name =\> ‘Category’,  
> :foreign\_key =\> 'parent\_id  
> end

Right - so Relationship is an AR model - it _needs_ an ID column.

There might be some other issues… but get to them after the  
Relationship models are working (try creating one in the console to  
make sure it works…)

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [July 4, 2010, 8:39pm UTC](https://www.ruby-forum.com/t/self-reference-almost-there-i-hope/190450/3 "2010-07-04T20:39:39Z")

</div>

On 20/06/2010 20:18, Michael P. wrote:

> > end
> 
> Right - so Relationship is an AR model - it _needs_ an ID column.

Also, you need to refer to the relationships association in the Category  
class as follows:

has\_many :parents, :class\_name =\> ‘Category’, :through =\>  
:relationships, :source =\> :parent

(In case there’s anybody still interested 🙂 )
