我在激活设备后遇到了奇怪的问题。带有Paperclip属性 has_mongoid_attached_file 的模型返回异常:
undefined method `after_commit' for Item:Class
/home/student/.rvm/gems/ruby-2.1.0/bundler/gems/paperclip-c9c5227cb7f8/lib/paperclip/has_attached_file.rb:89:in `add_active_record_callbacks'
/home/student/.rvm/gems/ruby-2.1.0/bundler/gems/paperclip-c9c5227cb7f8/lib/paperclip/has_attached_file.rb:19:in `define'
/home/student/.rvm/gems/ruby-2.1.0/bundler/gems/paperclip-c9c5227cb7f8/lib/paperclip/has_attached_file.rb:4:in `define_on'
/home/student/.rvm/gems/ruby-2.1.0/bundler/gems/paperclip-c9c5227cb7f8/lib/paperclip.rb:179:in `has_attached_file'
mongoid-paperclip (0.0.8) lib/mongoid_paperclip.rb:70:in `has_mongoid_attached_file'
据我理解,这个问题涉及到gems/paperclip-3.5.3/lib/paperclip/has_attached_file.rb中的after_commit的可见性。
def add_active_record_callbacks
name = @name
@klass.send(:after_save) { send(name).send(:save) }
@klass.send(:before_destroy) { send(name).send(:queue_all_for_delete) }
@klass.send(:after_commit, :on => :destroy) { send(name).send(:flush_deletes) }
end
在Gemfile中,我尝试了包括strong_parameters和protected_attributes在内的各种gem,但没有取得任何进展。
编辑: 我注释掉了这一行: @klass.send(:after_commit, :on => :destroy) { send(name).send(:flush_deletes) } 现在正确实现了mongoid中的paperclip
编辑: 在https://github.com/thoughtbot/paperclip/pull/1425/files中找到更好的解决方案。