0
可以說我有一個帖子,這篇文章應該有一定數量的評論,這些評論應該由certian用戶創建。因此,這些都是我的DataMapper模式:同時Datamapper兩個協會
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
所以我們可以說用戶X創建帖子和用戶Y想要創建這個信息的評論中。那我該怎麼做呢?我需要的是這樣的:
user = User.get(sessions[:user_id])
post = Post.get(params[:post_id])
comment = post.user.Comment.new {
:text => "Bla",
[...]
}
[...]
comment.save
[...]
所以basicly模型後應與模型評論和型號後有關,我怎麼知道呢?
我必須改變模型嗎?或者我現在可以離開他們嗎? –
模型沒問題。 – ujifgc