2012-01-17 57 views
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

+0

是DISPLAY_NAME'Comment'或'User'的領域? – 2012-01-17 19:22:17

+0

那麼..它是'User'的一部分,它是'Comment'的'EmbeddedDocument'。我正在使用Mongo_Mapper – Shamoon 2012-01-17 19:23:53

+1

我不知道如何使用MongoMapper,但我認爲你必須通過'newComment.user.display_name'來訪問它 – 2012-01-17 19:27:25

回答

1

披露:我從來沒有使用MongoMapper

所以我最好的選擇是:只需要添加display_nameComment模式的一部分。在模型/ comment.rb:

class Comment 
    include MongoMapper::Document 

    key :display_name, String 
    key ... 
    [...] 

相關問題