无法修改不可变资源(DataMapper :: ImmutableError)

时间:2011-10-16 22:40:28

标签: ruby datamapper

我有一个存储在数据库中的评论列表。我可以从数据库中提取注释并对其进行操作,但是,当我尝试通过datamapper再次更新数据库时,我收到错误。

我收到以下错误:

/usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.1.0/lib/dm-core/resource/state/immutable.rb:16:in `set': Immutable resource cannot be modified (DataMapper::ImmutableError)
from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.1.0/lib/dm-core/model/property.rb:238:in `body='
from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.1.0/lib/dm-core/resource.rb:334:in `block in attributes='
from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.1.0/lib/dm-core/resource.rb:330:in `each'
from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.1.0/lib/dm-core/resource.rb:330:in `attributes='
from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.1.0/lib/dm-core/resource.rb:379:in `update'
from /usr/local/lib/ruby/gems/1.9.1/gems/dm-validations-1.1.0/lib/dm-validations.rb:75:in `block in update'
from /usr/local/lib/ruby/gems/1.9.1/gems/dm-validations-1.1.0/lib/dm-validations/support/context.rb:30:in `validation_context'
from /usr/local/lib/ruby/gems/1.9.1/gems/dm-validations-1.1.0/lib/dm-validations.rb:75:in `update'
from process.rb:32:in `block (2 levels) in <main>'
from process.rb:28:in `each_line'
from process.rb:28:in `block in <main>'
from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.1.0/lib/dm-core/collection.rb:507:in `block in each'
from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.1.0/lib/dm-core/support/lazy_array.rb:411:in `block in each'
from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.1.0/lib/dm-core/support/lazy_array.rb:411:in `each'
from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.1.0/lib/dm-core/support/lazy_array.rb:411:in `each'
from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.1.0/lib/dm-core/collection.rb:504:in `each'
from process.rb:27:in `<main>'

这是代码:

comments.each do |comment|
  comment.body.each_line do |comment_line|
    comment_line = comment_line.lstrip
      if comment_line[0]== '>'
        new_comment = comment.body.slice! comment_line
        comment.update(:body => new_comment)
        comment.save
      end
   end
 end 

我做错了什么?

1 个答案:

答案 0 :(得分:1)

无需致电comment.save - 删除它,它可能会起作用