# Self-referential joins with :through raises TypeError

**URL:** <https://www.ruby-forum.com/t/self-referential-joins-with-through-raises-typeerror/156775>\
**Category:** Rails\
**Created:** [January 2, 2009, 6:36pm UTC](https://www.ruby-forum.com/t/self-referential-joins-with-through-raises-typeerror/156775 "2009-01-02T18:36:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [January 2, 2009, 6:36pm UTC](https://www.ruby-forum.com/t/self-referential-joins-with-through-raises-typeerror/156775/1 "2009-01-02T18:36:39Z")

</div>

I have a simple referral system composed of Users and Referrals.

The users table has an id value, of course. The referrals table has  
referer\_id and referee\_id. The relationships are set up as follows:

class User \< ActiveRecord::Base  
has\_many :referrals,  
:foreign\_key =\> “referer\_id”

has\_one :referer,  
:through =\> :referrals,  
:source =\> :referer

has\_many :referees,  
:through =\> :referrals,  
:source =\> :referee

class Referral \< ActiveRecord::Base  
belongs\_to :referer,  
:class\_name =\> :user,  
:foreign\_key =\> “referer\_id”

belongs\_to :referee,  
:class\_name =\> :user,  
:foreign\_key =\> “referee\_id”

Most of this works. User.referrals returns all the referral objects.  
Referral.referer and Referral.referee both return the appropriate  
objects. But the User.referer/referee methods both raise the same  
error:

> > user.referer  
> > TypeError: can’t convert Symbol into String

I would like User.referer to return the single user object that is their  
referer. And User.referees would return an array of user objects that  
were refered. What am I doing wrong here?

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [January 2, 2009, 6:40pm UTC](https://www.ruby-forum.com/t/self-referential-joins-with-through-raises-typeerror/156775/2 "2009-01-02T18:40:17Z")

</div>

On 2 Jan 2009, at 17:36, Taylor S. wrote:

> Most of this works. User.referrals returns all the referral objects.  
> Referral.referer and Referral.referee both return the appropriate  
> objects. But the User.referer/referee methods both raise the same  
> error:

The class\_name option should really be a string (ie :class\_name =\>  
‘User’)

Fred

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [January 2, 2009, 6:53pm UTC](https://www.ruby-forum.com/t/self-referential-joins-with-through-raises-typeerror/156775/3 "2009-01-02T18:53:12Z")

</div>

Frederick C. wrote:

> On 2 Jan 2009, at 17:36, Taylor S. wrote:
> 
> > Most of this works. User.referrals returns all the referral objects.  
> > Referral.referer and Referral.referee both return the appropriate  
> > objects. But the User.referer/referee methods both raise the same  
> > error:
> 
> The class\_name option should really be a string (ie :class\_name =\>  
> ‘User’)
> 
> Fred

Thanks that solved the issue.

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [January 2, 2009, 7:22pm UTC](https://www.ruby-forum.com/t/self-referential-joins-with-through-raises-typeerror/156775/4 "2009-01-02T19:22:33Z")

</div>

Taylor S. wrote:

> Frederick C. wrote:
> 
> > On 2 Jan 2009, at 17:36, Taylor S. wrote:
> > 
> > > Most of this works. User.referrals returns all the referral objects.  
> > > Referral.referer and Referral.referee both return the appropriate  
> > > objects. But the User.referer/referee methods both raise the same  
> > > error:
> > 
> > The class\_name option should really be a string (ie :class\_name =\>  
> > ‘User’)

Actually I spoke too soon. User.referer always returns nil, even if  
there is a referral record with appropriate referee\_id and referer\_id.  
Also, if I do user1.referer = user2 a NoMethodError is raised:

NoMethodError: undefined method `update\_attributes’ for  
#Class:0x237de24

User.referees does correctly return an array of user objects, though.

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [January 2, 2009, 8:26pm UTC](https://www.ruby-forum.com/t/self-referential-joins-with-through-raises-typeerror/156775/5 "2009-01-02T20:26:28Z")

</div>

The SQL generated by user1.referer is

SELECT “users”.\* FROM “users” INNER JOIN referrals ON users.id =  
referrals.referer\_id WHERE ((“referrals”.referer\_id = 1))

But it should be using WHERE referrals.referee\_id = 1 instead of  
referer\_id = 1. I’ve tinkered with various permutations but cannot make  
it query for users.id = referer\_id WHERE referee\_id = x. Any thoughts?

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [January 5, 2009, 8:21pm UTC](https://www.ruby-forum.com/t/self-referential-joins-with-through-raises-typeerror/156775/6 "2009-01-05T20:21:45Z")

</div>

Never fixed it ☹

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [January 5, 2009, 6:41pm UTC](https://www.ruby-forum.com/t/self-referential-joins-with-through-raises-typeerror/156775/7 "2009-01-05T18:41:01Z")

</div>

Hi Taylor, Did you solve this problem? I have exactly the same. Greg

On 2 Jan, 20:26, Taylor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [January 5, 2009, 10:06pm UTC](https://www.ruby-forum.com/t/self-referential-joins-with-through-raises-typeerror/156775/8 "2009-01-05T22:06:15Z")

</div>

On Jan 5, 7:21 pm, Taylor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Never fixed it ☹

If referrals are all the referrals made by that user (since it uses  
the referer\_id foreign key) it seems to me that will never get you a  
user’s referer (unless the use referred themselves).

You’d want to go through the referrals table using the other key.

Fred

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [January 5, 2009, 10:00pm UTC](https://www.ruby-forum.com/t/self-referential-joins-with-through-raises-typeerror/156775/9 "2009-01-05T22:00:57Z")

</div>

## [frozenplague.net](http://frozenplague.net/2008/04/self-referrential-relationships/)

Ryan B.  
Freelancer

> **[frozenplague.net](http://frozenplague.net)**
>
> This domain may be for sale!
