在我的照片班中,我有这种关联。Rails如何在不锁定的情况下触摸活动记录对象?
belongs_to :user, :touch => true
有一天我得到了这个例外。
A ActiveRecord::StatementInvalid occurred in photos#update:
Mysql::Error: Deadlock found when trying to get lock; try restarting transaction:
UPDATE `users` SET `updated_at` = '2011-09-20 14:17:44' WHERE `users`.`id` = 6832
production/ruby/1.8/gems/activerecord-3.0.10/lib/active_record/connection_adapters/abstract_adapter.rb:207:in `log'
我应该怎么做才能防止这种情况发生?如果可能,我希望错误中显示的更新语句不使用锁定。我不认为在这种情况下使用乐观锁定会起作用,因为乐观锁定可能会引发ActiveRecord :: StaleObjectError。
感谢您的回答。这是2年后,我不完全确定我为什么使用触摸。使缓存失效似乎是一个很好的理由。我现在使用清理器过期缓存条目。 http://guides.rubyonrails.org/caching_with_rails.html#sweepers。我不再使用触摸了。 –
我喜欢valid_from的想法,但我认为你需要在cache_key中添加类名,例如[class]/[id] - [timestamp]按照http://signalvnoise.com/posts/3113-如何琴键基于缓存过期,作品 – iheggie