0
@comments = []
@preComments = Comment.where(:resource_hash => resource_hash).
sort(:created_at.desc).all
@preComments.each do |comment|
newComment = comment
u = ::User.find_by_id comment.user_id
newComment.display_name = "Some name"
if u.image_location.nil?
newComment.image_location = "defaultpic"
else
newComment.image_location = u.image_location
end
p u
@comments << newComment
p "HERE!!!!!"
end
這是我的代碼,但我得到一個錯誤說如何在Rails 3.1中分配一個對象的新元素?
undefined method `display_name=' for #
那麼,如何分配display_name
?
是DISPLAY_NAME'Comment'或'User'的領域? – 2012-01-17 19:22:17
那麼..它是'User'的一部分,它是'Comment'的'EmbeddedDocument'。我正在使用Mongo_Mapper – Shamoon 2012-01-17 19:23:53
我不知道如何使用MongoMapper,但我認爲你必須通過'newComment.user.display_name'來訪問它 – 2012-01-17 19:27:25