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.refererTypeError: 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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Jan-02 17:39 UTC
Re: self-referential joins with :through raises TypeError
On 2 Jan 2009, at 17:36, Taylor Strait wrote:> > 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: >The class_name option should really be a string (ie :class_name => ''User'') Fred>>> 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? > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Taylor Strait
2009-Jan-02 17:53 UTC
Re: self-referential joins with :through raises TypeError
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'') > > FredThanks that solved the issue. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Taylor Strait
2009-Jan-02 18:22 UTC
Re: self-referential joins with :through raises TypeError
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Taylor Strait
2009-Jan-02 19:26 UTC
Re: self-referential joins with :through raises TypeError
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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Taylor Strait
2009-Jan-05 19:21 UTC
Re: self-referential joins with :through raises TypeError
Never fixed it :( -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
http://frozenplague.net/2008/04/self-referrential-relationships/ ----- Ryan Bigg Freelancer http://frozenplague.net On 06/01/2009, at 5:51 AM, Taylor Strait wrote:> > Never fixed it :( > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Jan-05 21:05 UTC
Re: self-referential joins with :through raises TypeError
On Jan 5, 7:21 pm, Taylor Strait <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---