rails: self-referential association

Posted by john on Stack Overflow See other posts from Stack Overflow or by john
Published on 2010-06-12T15:55:45Z Indexed on 2010/06/12 16:03 UTC
Read the original article Hit count: 139

hi,

My needs are very simple: I have a Tip table to receive comments and have comments to receive comments, too.

To retrieve each comment that is stored in the same table (comments), I created another key for the comments on comments: "inverse_comments".

I tried to use one comments table by using self-referntial association. Some resources seem to bring more than one table into the piture which are diffent from my needs. So I came up whth the following modeling for comments:

class Comment < ActiveRecord::Base
  belongs_to :tip 
  belongs_to :user
  has_many :mycomments, 
           :through => :inverse_comments,
           :source => :comment
end

Apparently something is missing here but I cannot figure it out. Could some one enlighten me on this:

what changes I need to do to make the model work?

thanks.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about self-reference