My :post
model is the parent model. Within a post are many comments. And comments have potential of having many more comments, and so forth. I thought I had had this right, but I were to type :
Comment.last.comments
I get :
NoMethodError: undefined method `comments' for #<Comment:0x1053a1ff0>
My Models :
#comment.rb
belongs_to :post
belongs_to :parent, :class_name => 'Comment'
has_many :children, :class_name => 'Comment'
validates_presence_of :text
#post.rb
has_many :comments
accepts_nested_attributes_for :comments, :allow_destroy => true
My Migration
class CreateComments < ActiveRecord::Migration
def self.up
create_table :comments do |t|
t.text :text
t.integer :post_id
t.integer :comment_id
t.timestamps
end
end
def self.down
drop_table :comments
end
end