【发布时间】:2014-01-30 03:45:42
【问题描述】:
我在设备激活后遇到了奇怪的问题。带有回形针属性的模型 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 中,我玩过各种 gem,包括 strong_parameters 和 protected_attributes,但没有任何进展。
编辑: 我评论行 @klass.send(:after_commit, :on => :destroy) { send(name).send(:flush_deletes) } 和问题现在用 mongoid 正确实现回形针
编辑: 在https://github.com/thoughtbot/paperclip/pull/1425/files找到更好的解决方案
【问题讨论】:
标签: ruby-on-rails devise mongoid paperclip