# Newbie trying self-referencing :through via REST model

**URL:** <https://www.ruby-forum.com/t/newbie-trying-self-referencing-through-via-rest-model/156951>\
**Category:** Rails\
**Created:** [January 5, 2009, 6:15pm UTC](https://www.ruby-forum.com/t/newbie-trying-self-referencing-through-via-rest-model/156951 "2009-01-05T18:15:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![itsastickup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7feea3/32.png) [@itsastickup](https://www.ruby-forum.com/u/itsastickup)\
**Post date:** [January 5, 2009, 6:15pm UTC](https://www.ruby-forum.com/t/newbie-trying-self-referencing-through-via-rest-model/156951/1 "2009-01-05T18:15:37Z")

</div>

I’ve read through countless blogs and the docs and can’t seem to get  
what I expect. I also tried irc on freenode, but they ignored me. I  
must have a case of the mange or something.

I have a Person model (on a people table) and I want a hierarchy of  
masters and servants. I’m doing this via a Vassalship model. When I  
try to assign a master to a servant like so :

```
 juniorperson.master=seniorperson

```

I get NoMethodError: undefined method `update\_attributes’ for #\<Class:  
0x48be738\>

And who knows what class that was. I can do  
seniorperson.servants\<\<juniorperson no problem, but then I get a nil  
for juniorperson.master ?!?!

My models look like this :

class Person \< ActiveRecord::Base

has\_many :vassalships,:class\_name=\>‘Vassalship’,:foreign\_key=\>:master\_id  
has\_one :master,:through=\>:vassalships,:source=\>:master;  
has\_many :servants,:through=\>:vassalships,:source=\>:servant;  
end

class Vassalship \< ActiveRecord::Base  
belongs\_to :master,:class\_name=\>‘Person’,:foreign\_key=\>‘master\_id’  
belongs\_to :servant,:class\_name=\>‘Person’,:foreign\_key=\>‘servant\_id’  
end

my tables are here, if you need to see them: [http://pastie.org/352858](http://pastie.org/352858),  
and the full console track is here : [http://pastie.org/352871](http://pastie.org/352871)

I’m doing something wrong, but I don’t know what. And anyone got a  
better idea for the relationship? Vassalship seems a bit obscure.

---

<div class="post-metadata">

**Author:** ![itsastickup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7feea3/32.png) [@itsastickup](https://www.ruby-forum.com/u/itsastickup)\
**Post date:** [January 5, 2009, 10:46pm UTC](https://www.ruby-forum.com/t/newbie-trying-self-referencing-through-via-rest-model/156951/2 "2009-01-05T22:46:07Z")

</div>

On Mon, Jan 5, 2009 at 6:14 PM, itsastickup [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> belongs_to :master,:class_name=>'Person',:foreign_key=>'master_id'
> belongs_to :servant,:class_name=>'Person',:foreign_key=>'servant_id'
> 
> ```
> 
> end

You only need one model, Person should suffice:

class Person \< ActiveRecord::Base

belongs\_to :master, :class\_name =\> ‘Person’, :foreign\_key =\>  
‘master\_id’

has\_many :servants, :class\_name =\> ‘Person’, :foreign\_key =\>  
‘master\_id’

end

Let me know how you get on.

Regards,  
Franz

---

<div class="post-metadata">

**Author:** ![itsastickup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7feea3/32.png) [@itsastickup](https://www.ruby-forum.com/u/itsastickup)\
**Post date:** [January 6, 2009, 11:01am UTC](https://www.ruby-forum.com/t/newbie-trying-self-referencing-through-via-rest-model/156951/3 "2009-01-06T11:01:52Z")

</div>

I have a need for REST (as well as other things), as mentioned, and  
for that I require the go-between model. In anycase presumably my  
original question does have an answer, no? Perhaps :through does not  
work self-referentially, although I would be surprised. But the docs  
don;t have it and blogs haven’t helped.

On 5 Jan, 22:45, “Franz S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

---

<div class="post-metadata">

**Author:** ![itsastickup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7feea3/32.png) [@itsastickup](https://www.ruby-forum.com/u/itsastickup)\
**Post date:** [January 7, 2009, 3:28pm UTC](https://www.ruby-forum.com/t/newbie-trying-self-referencing-through-via-rest-model/156951/4 "2009-01-07T15:28:51Z")

</div>

try:

class Person \< ActiveRecord::Base

has\_many  
:vassalships,:class\_name=\>‘Vassalship’,:foreign\_key=\>‘master\_id’  
has\_many :vassalship\_belongings, :class\_name =\>  
‘Vassalship’, :foreign\_key =\> ‘slave\_id’  
has\_one :master,:through=\>:vassalships,:source=\>:person

has\_many :servants,:through=\>:vassalship\_belongings,:source=\>:person  
end

class Vassalship \< ActiveRecord::Base

belongs\_to :master,:class\_name=\>‘Person’,:foreign\_key=\>‘master\_id’

belongs\_to :servant,:class\_name=\>‘Person’,:foreign\_key=\>‘servant\_id’  
end
