Forum: Ruby on Rails self_referencial!!!

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ce953bec3af375ddc75e375233112b28?d=identicon&s=25 harper (Guest)
on 2007-01-21 17:26

i'm getting really frusterated after a good two days of racking my
brains on this subject; i went through josh susser's hasmanythrough
blog, and digged up the four pages all over the net about
self_referencial associations (very bad documentation, on ror's side)
and i can't seem to figure this out.

i've got a Tag model, just like any other Tag models go, but aside from
that, i want to associate a tag to itself, (just like making a user some
other user's friend) so that could to something like:

undertags = tag.undertags # which is an array of tags...

in the database i added:
:table => "undertagships"
:columns => tag_id            # this is like a user
         => undertag_id       # this is like a friend
 class Tag
.    has_many :undertagships,
      :foreign_key =>       'tag_id',
      :class_name =>        'Undertagship'
    has_many :undertags,
    :through =>     :undertagships,
    :source =>  :b

 class Undertagship

       belongs_to :b,
         :foreign_key => "undertag_id",
         :class_name => "Tag"

walla! i can actually access some other tags via a tag object:

undertags = tag.undertags # which is an array of tags...

which is simply wondersplendiddifully great.
thing is, i don't have access to any of the other methods. i.e, i can't

@tag.undertags << array_of_undertags # ERROR - NO METHOD undertags=()
@tag.destroy # DOESN'T DESTROY ASSOCIATED undertags
@tag.undertags # no validation on creating Undertag.create{:tag_id => 1,

how can i access all of these write / destroy / change attributes? how
is it possible that i can access undertags via @tag.undertags, but can't
asign a value to them? (   !undertags=()  ) is this a known bug on
has_many through?

many many, many, thanks.

Ce953bec3af375ddc75e375233112b28?d=identicon&s=25 harper (Guest)
on 2007-01-22 14:28
hey; anyone? take the time for one sec to take a look?
many appreciations. if not, thx anyway.

This topic is locked and can not be replied to.