DataMapper 自引用模型

DataMapper Self Referential Model

我有一个模型:

class Comment
   include DataMapper::Resource
   property :id, Serial
   property :comment, Text, :required => true  
   belongs_to :user
   belongs_to :lecture
   has n, :replies, :child_key => [:source_id]
   has n, :comments, self, :through => :replies, :via => :target
end

我想添加一条评论作为对另一条已创建评论的回复。当我尝试时:

lecture = Lecture.get(params[:lecture_id])
comment = Comment.new(:comment => params[:text])  
@user.comments << comment
lecture.comments << comment 

if !params[:parent].nil? then
  parent = Comment.get(params[:parent])
  parent.replies << comment
end

parent.replies << comment行抛出错误:
NoMethodError - undefined method source_id=' for #<Comment @id=nil @comment="asd" @user_id=1 @lecture_id=1>

我的回复模型是:

class Reply 
 include DataMapper::Resource 
  belongs_to :source, 'Comment', :key => true
  belongs_to :target, 'Comment', :key => true
end

如何正确地将评论添加为 'reply'?谢谢。

您确定要 Reply 模型吗?评论树可以仅建立在一个具有自关联的模型 Comment 上。

class Comment
  ...
  has n, :replies, 'Comment', :child_key => :source_id
  belongs_to :source, 'Comment', :required => false
end