![](/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.