self-joins are discussed here:
http://guides.rubyonrails.org/association_basics.html#self-joins

self-referential association is discussed here:
http://railscasts.com/episodes/163-self-referential-association

The main difference I see is that self-referential association creates a
join model, such as friendship, which links another model, such as user, to
itself, so a user can have many friends (which are other users), and a
friend can be befriended by a user. The self-joins looks like there is no
join model. Simply a foreign key is added to the same model, such as a
manager_id column to the employee model. An employee, who is a manager, can
have many other employees, who are subordinates. And the link is done on
the same table itself, association the employee manager_id column with the
the employee id column. To me, these two techniques look virtually the
same. Is there a difference and which is preferred?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bdd46a05-4cbe-4db3-ac90-679ffbdfedd5%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Colin Law at Dec 7, 2013 at 8:48 pm

    On 7 December 2013 19:23, John Merlino wrote:
    self-joins are discussed here:
    http://guides.rubyonrails.org/association_basics.html#self-joins

    self-referential association is discussed here:
    http://railscasts.com/episodes/163-self-referential-association

    The main difference I see is that self-referential association creates a
    join model, such as friendship, which links another model, such as user, to
    itself, so a user can have many friends (which are other users), and a
    friend can be befriended by a user. The self-joins looks like there is no
    join model. Simply a foreign key is added to the same model, such as a
    manager_id column to the employee model. An employee, who is a manager, can
    have many other employees, who are subordinates. And the link is done on the
    same table itself, association the employee manager_id column with the the
    employee id column. To me, these two techniques look virtually the same. Is
    there a difference and which is preferred?
    With a join model the relationship is symetrical. Each user can have
    many friends and can be the friend of many other users, similar to a
    has_and_belongs_to_many association. With the self referential
    association you describe, a manager can have many employees but an
    employee can belongs to only one manager (like a has_many, belongs_to
    association). So which one to use depends on the requirements.

    Colin

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLuOKFb3e4JVoyTtkH0SJDurPSsxSXRwsjsGnPRWXy4pHQ%40mail.gmail.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedDec 7, '13 at 7:23p
activeDec 7, '13 at 8:48p
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Colin Law: 1 post John Merlino: 1 post

People

Translate

site design / logo © 2022 Grokbase