@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
这是我的代码,但我收到错误说
undefined method `display_name=' for #
那么如何分配display_name
?
答案 0 :(得分:1)
披露:我从未使用过MongoMapper
所以我最好的选择是:只需添加display_name
作为Comment
架构的一部分。在models / comment.rb中:
class Comment
include MongoMapper::Document
key :display_name, String
key ...
[...]
端