Как мне назначить новый элемент объекта в Rails 3.1? - PullRequest
0 голосов
/ 17 января 2012
@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?

1 Ответ

1 голос
/ 17 января 2012

Раскрытие информации: я никогда не использовал MongoMapper

Так что мой лучший выбор: просто добавьте display_name как часть Comment схемы.В моделях / comment.rb:

class Comment
  include MongoMapper::Document

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

end

...