![](/img/trans.png)
[英]How to capture errors from ActiveRecord::Base.connection.execute in Rails?
[英]How to capture all ActiveRecord::Errors under config/initializersin Rails3
我试图配置我的应用程序以从我的应用程序中的任何ActiveRecord对象打印错误。 这样我就可以避免在所有事务中为每个对象打印错误消息。 有什么办法吗?
需要这样的东西
在config / initializers中
ActiveRecord::Base.class_eval do
# For each active_record object in my app
if self.errors.any?
puts self.errors.inspect
end
end
我相信您希望将所有AR错误集中在一处,
一种方法是使用rescue_from
。 只需您可以在应用程序控制器中执行类似的操作
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
#your normal code
rescue_from Exception do |exeception|
#check if this is a AR exception
#if yes then
#log it in a different log file
#if not
# yield (to follow the default exception chain)
end
end
更合适的方法是将其包含在模块中并将其include
在ApplciationController
。
高温超导
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.