开发者

Immutable resource cannot be modified (DataMapper::ImmutableError)

开发者 https://www.devze.com 2023-04-13 04:51 出处:网络
I have a list of comments stored in a database. I can pull the comment from the database and manipulate it, however, I get the error when I try to update my database again via datamapper.

I have a list of comments stored in a database. I can pull the comment from the database and manipulate it, however, I get the error when I try to update my database again via datamapper.

I get the following error:

/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/开发者_如何学Pythonruby/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>'

And here's the code:

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 

What am I doing wrong?


No need to call comment.save - remove it and it's possible it will work

0

精彩评论

暂无评论...
验证码 换一张
取 消