0

Lets say I have a post, this post should have a certian amount of comments, and these comments should be created by certian users. So these are my datamapper models:

class User
include DataMapper::Resource

property :id, Serial
property :name, String,
property :password, String

has n, :post
end

class Post
include DataMapper::Resource

property :id, Serial
property :text, Text
property :created_at, DateTime

belongs_to :user
end

class Comment
include DataMapper::Resource

property :text, Text,
property :created_at, DateTime

belongs_to :post
belongs_to :user
end

So let's say user x creates a post and user y wants to create a comment to this post. How do I do this then? I need something like this:

user = User.get(sessions[:user_id])
post = Post.get(params[:post_id])
comment = post.user.Comment.new {
:text => "Bla",
[...]
}

[...]

comment.save

[...]

So basicly the Model Post should be associated with the Model Comment and the Model Post, how do I realize this?

Micheal Perr
  • 1,425
  • 5
  • 14
  • 23

1 Answers1

3
comment = Comment.create :post => post, :user => user, :text => 'Foo'
ujifgc
  • 2,075
  • 1
  • 18
  • 20