I am busy investigating what docs I can lay my hands on, but in the meantime, maybe the list can help: I am busy designing a referral system where my members will be able to refer one another. So I need to be able to establish a cross reference within my model to other records in the model. What would be the beest way of doing this? I am thinking along the lines of: class Member < ActiveRecord::Base belongs_to :Member ... ... but not sure how to go about this, and also how to define the "_id" attributes of the members table to have Rails knit it together automagically for me. Any words from the wise out there? H.
Rodrigo Alvarez Fernández
2005-Oct-27 22:03 UTC
Re: Self referential Models - how to code?
On 10/27/05, Hendie Dijkman <ruby-Vki3jUAxBCcyzzc7d281tmfPcZCv/ETZ@public.gmane.org> wrote:> I am busy investigating what docs I can lay my hands on, but in the > meantime, maybe the list can help: > > I am busy designing a referral system where my members will be able to > refer one another. So I need to be able to establish a cross reference > within my model to other records in the model. What would be the beest > way of doing this? I am thinking along the lines of: > > class Member < ActiveRecord::Base > belongs_to :Member > ... > > ... but not sure how to go about this, and also how to define the > "_id" attributes of the members table to have Rails knit it together > automagically for me. > > Any words from the wise out there?http://wiki.rubyonrails.com/rails/pages/HowToUseManyToManyAgainstASingleTable
> I am busy designing a referral system where my members will be able to > refer one another. So I need to be able to establish a cross reference > within my model to other records in the model. What would be the beest > way of doing this? I am thinking along the lines of: > > class Member < ActiveRecord::Base > belongs_to :Member > ... > > ... but not sure how to go about this, and also how to define the > "_id" attributes of the members table to have Rails knit it togetherThe way associations work is that when you access them (for example, member.referrer) AR just needs to know which AR class it should look the association up in, and what key it should use to find the proper record. Normally, if you want to look up "referrer", AR assumes the class will be "Referrers" (or something like that), and that the foreign key to use will be "referrer_id". That won''t work here because you want "referrer" to point back into the members table. Here''s what you do: The members database table needs a column called "referrer_id" that contains the id of another member record--it points to the member that referred this member. In the model: class Member < ActiveRecord::Base belongs_to :referrer, :class_name => "Member", :foreign_key => "referrer_id" end That''s it. . . now, if member is a member object, member.referrer will be another member object. For example, if Fred referred Alice, you could do something like: member = Member.find_by_name("Alice") member.referrer = Member.find_by_name("Fred") member.save --Forrest