简体   繁体   English

尝试删除Heroku上的记录时,如何解决“ RuntimeError:无法修改冻结的哈希”?

[英]How do I work around a “RuntimeError: can't modify frozen Hash” when trying to delete records on Heroku?

At my Rails console on Heroku, I am trying to do a simple array iteration and delete the record if a certain condition exists, and I am running into that error. 在Heroku上的Rails控制台上,我尝试执行简单的数组迭代并在存在特定条件的情况下删除记录,并且遇到了该错误。

This is what I am doing: 这就是我在做什么:

irb(main):044:0> a.first
=> #<Activity id: 1, trackable_id: 3, trackable_type: "Node", owner_id: 5, owner_type: "User", key: "node.create", parameters: {}, recipient_id: nil, recipient_type: nil, created_at: "2014-07-30 11:22:15", updated_at: "2014-07-30 11:22:15", read_status: 0>
irb(main):045:0> a.first.trackable.nil?
=> false
irb(main):046:0> a.second.trackable.nil?
=> true
irb(main):061:0> a.each do |x|
irb(main):062:1* if x.trackable.nil?
irb(main):063:2> x.destroy
irb(main):064:2> x.save
irb(main):065:2> end
irb(main):066:1> end
RuntimeError: can't modify frozen Hash

Thoughts on how I can achieve this? 关于如何实现此目标的想法?

If I leave off the x.save it doesn't get rid of the record it seems. 如果我放弃了x.save它似乎不会摆脱记录。

You can't call .save on a destroyed record. 您不能在已破坏的记录上调用.save Once you've called .destroy on the record, it's been removed from the database already; 在记录中调用.destroy ,它已经从数据库中删除了; there's no use calling .save on it. 调用.save没用。 If you're basing your determination of whether it's been destroyed by whether you still have access to it in Ruby's memory, you can call .destroyed? 如果要根据是否仍可以在Ruby的内存中访问它来确定它是否已被破坏,可以致电.destroyed? on a record to find out whether it's been destroyed or not. 记录下来以了解它是否已被销毁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM