Reputation: 167
I have a 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
And I want to add a comment as a reply to another, already created comment. When I try:
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
The line parent.replies << comment
throws the error:
NoMethodError - undefined method source_id=' for #<Comment @id=nil @comment="asd" @user_id=1 @lecture_id=1>
My Reply model is:
class Reply
include DataMapper::Resource
belongs_to :source, 'Comment', :key => true
belongs_to :target, 'Comment', :key => true
end
How do I correctly add a comment as a 'reply'? Thanks.
Upvotes: 0
Views: 112
Reputation: 2245
Are you sure you want Reply
model? The comment tree can be build on just one model Comment
that has self-association.
class Comment
...
has n, :replies, 'Comment', :child_key => :source_id
belongs_to :source, 'Comment', :required => false
end
Upvotes: 1