Forum: Ruby on Rails self-referential joins with :through raises TypeError

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8e7e5b934fcbcdfb475072685755457f?d=identicon&s=25 Taylor Strait (taylorstrait)
on 2009-01-02 18:36
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?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-01-02 18:40
(Received via mailing list)
On 2 Jan 2009, at 17:36, Taylor Strait 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
8e7e5b934fcbcdfb475072685755457f?d=identicon&s=25 Taylor Strait (taylorstrait)
on 2009-01-02 18:53
Frederick Cheung wrote:
> On 2 Jan 2009, at 17:36, Taylor Strait 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.
8e7e5b934fcbcdfb475072685755457f?d=identicon&s=25 Taylor Strait (taylorstrait)
on 2009-01-02 19:22
Taylor Strait wrote:
> Frederick Cheung wrote:
>> On 2 Jan 2009, at 17:36, Taylor Strait 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.
8e7e5b934fcbcdfb475072685755457f?d=identicon&s=25 Taylor Strait (taylorstrait)
on 2009-01-02 20:26
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?
2701c824d560c66c31b3d58d36fe17cf?d=identicon&s=25 itsastickup (Guest)
on 2009-01-05 18:41
(Received via mailing list)
Hi Taylor, Did you solve this problem? I have exactly the same. Greg

On 2 Jan, 20:26, Taylor Strait <rails-mailing-l...@andreas-s.net>
8e7e5b934fcbcdfb475072685755457f?d=identicon&s=25 Taylor Strait (taylorstrait)
on 2009-01-05 20:21
Never fixed it :(
9a2a53db8e9b4476038c94a64b32833f?d=identicon&s=25 Ryan Bigg (ryan-bigg)
on 2009-01-05 22:00
(Received via mailing list)
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-01-05 22:06
(Received via mailing list)
On Jan 5, 7:21 pm, Taylor Strait <rails-mailing-l...@andreas-s.net>
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
This topic is locked and can not be replied to.