问题描述
我有一个 Rails 3.0.9 应用程序,一旦部署,就会遇到一堆 ActiveModel::MissingAttributeErrors,导致 500 秒.错误发生相当随机,有时页面会加载,有时不会,但属性都是数据库中现有的属性,应该找到.
I have a Rails 3.0.9 app that, once it is deployed, suffers from a bunch of ActiveModel::MissingAttributeErrors that crop up causing 500s. The errors occur fairly randomly, sometimes a page will load, other times it won't, but the attributes are all existing attributes in the database and should be found.
奇怪的是,过了一会儿,错误就会消失.突然,它们不再引起问题.
The strange part is that after a while, the errors go away. Suddenly, they stop causing an issue.
我已经搜索了解决方案,但是当有人完成了 Model.all(:select => 'column_x,column_y')
并且正在调用 Model.all(:select => 'column_x,column_y')
code>column_z 或当他们使用 cache_money 时.这些事情我都没有做.
I have searched about for a solution to this, but this error mostly occurs either when someone has done Model.all(:select => 'column_x,column_y')
and are calling for column_z
or when they are using cache_money. I am doing neither of these things.
有人可以帮忙吗?
推荐答案
您可能有一个查询没有返回所有列(即使用 :select
)然后 cache_money;或其他一些 ActiveRecord 插件使用 after_initialize
回调,每当创建新的 ActiveRecord 对象时(即从数据库中获取时)都会执行该回调.
You probably have a query that doesn't return all the columns (i.e. uses :select
) and then cache_money; or some other ActiveRecord plugin uses an after_initialize
callback, which executes whenever a new ActiveRecord object is created (i.e. when fetched from the database).
在那个初始化回调中,某些东西试图访问或使用未包含在 :select
中的属性.您希望该属性为该属性返回 nil,但会抛出 ActiveRecord::MissingAttributeError.
In that initialize callback, something tries to access or use an attribute that wasn't included in the :select
. You'd expect this to return nil for that attribute, but an ActiveRecord::MissingAttributeError is thrown instead.
您可以像文章建议的那样拯救 ActiveRecord::MissingAttributeError,或者在插件尝试访问或修改属性之前修补插件以使用 has_attribute?(:attribute_name)
.
You can rescue ActiveRecord::MissingAttributeError like the article suggests, or patch the plugin(s) to use has_attribute?(:attribute_name)
before they try to access or modify the attribute.
这篇关于ActiveModel::MissingAttributeError 在部署后发生,一段时间后消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!