ActiveModel::MissingAttributeError 在部署后发生,然后在一段时间后消失

发布于 2024-11-26 05:26:15 字数 359 浏览 2 评论 0原文

我有一个 Rails 3.0.9 应用程序,一旦部署,就会遇到一堆 ActiveModel::MissingAttributeErrors ,这些错误会突然出现,导致 500 秒。这些错误的发生相当随机,有时会加载页面,有时则不会,但这些属性都是数据库中现有的属性,应该可以找到。

奇怪的是,一段时间后,错误就消失了。突然间,他们不再制造问题了。

我已经搜索过此问题的解决方案,但此错误大多发生在有人完成 Model.all(:select => 'column_x,column_y') 并调用 column_z 时 或者当他们使用cache_money 时。我没有做这两件事。

有人可以帮忙吗?

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.

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.

Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

奈何桥上唱咆哮 2024-12-03 05:26:15

您可能有一个不返回所有列的查询(即使用:select),然后返回cache_money;或其他一些 ActiveRecord 插件使用 after_initialize 回调,每当创建新的 ActiveRecord 对象时(即从数据库获取时)都会执行该回调。

在该初始化回调中,某些内容尝试访问或使用 :select 中未包含的属性。您希望该属性返回 nil,但实际上会抛出 ActiveRecord::MissingAttributeError。

您可以像文章建议的那样挽救 ActiveRecord::MissingAttributeError,或者在插件尝试访问或修改属性之前修补插件以使用 has_attribute?(:attribute_name)

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).

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.

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.

零時差 2024-12-03 05:26:15

如果您仅在更新数据库后直接遇到此问题,而没有进行任何部署或服务器重新启动,那么对我有用的方法可能对您也有用:

运行 heroku restart ,它应该可以修复。在测功机重新启动之前,旧数据有时仍缓存在服务器上,因此再次启动将清除所有数据并防止其导致此类错误。希望这有帮助。

If you have been having this issue only directly after updating your database without any deploys or server restarts following, then what worked for me may work for you:

Run heroku restart and it should be fixed. Before the dyno restarts old data sometimes remains cached on the server, so starting it up again will scrub all of that data and prevent it from causing errors of that sort. Hope this helps.

少女净妖师 2024-12-03 05:26:15

我遇到了这个问题。确保您的 select: 包含视图中引用的所有字段,包括任何关系 ID 以及在您的方法中调用的任何属性。

当你的观点和关系很复杂时,缺失的属性就很难识别。调试此问题的最简单方法是删除 where 子句的 select 部分,并查看查询/范围/方法是否正确运行。如果是这样,则将所有属性添加到 select 中,并一次删除一个不需要的属性,直到找到有问题的属性。

I encountered this issue. Make sure your select: includes all fields referenced in your view, including any relationship IDs and any attributes called within your methods.

The missing attribute can be difficult to identify whenever your views and relationships are complex. The easiest way to debug this is to remove the select portion of your where clause and see if the query/scope/method runs correctly. If so, then add all of the attributes to the select and remove unneeded attributes one-at-a-time until you find the offending attribute.

云归处 2024-12-03 05:26:15

我发现一个有趣的做法导致了同样的错误。为了重用代码,我们使用执行分组以在图形视图中使用的演示者类对演示者类进行子类化。

为了简化,它是这样的:

class PostPresenter 
  def query
    Post.where(...stuff....).includes(:wombat)
  end
end

聚合器做了如下的事情来构建每天的帖子表:

class AggregatePostPresenter < PostPresenter
  def group_query
    query.select('count(*) as cnt, date(created_at)').group('date(created_at)')
  end
end

对“group_query”的调用会导致 ActiveModel::MissingAttributeError 因为我认为尝试“包括”Wombat失败,因为“wombat_id”不在“select”中包含的属性中。

然而,这可能不是您的答案,因为无论是否启用缓存,它都会发生。

I found an interesting take on this that resulted in the same error. In an attempt to reuse code we subclasses a presenters class with a presenters class that performed grouping to use in a graph view.

To simplify, it was something like:

class PostPresenter 
  def query
    Post.where(...stuff....).includes(:wombat)
  end
end

The the aggregator did something like the following to build a table of posts per day:

class AggregatePostPresenter < PostPresenter
  def group_query
    query.select('count(*) as cnt, date(created_at)').group('date(created_at)')
  end
end

A call to "group_query" results in an ActiveModel::MissingAttributeError since, I think, the attempt to "includes" Wombat fails because "wombat_id" wasn't in the attributes included in the "select".

This is probably not your answer, however, since it happens regardless of whether or not cache is enabled.

兔姬 2024-12-03 05:26:15

当我尝试进行 Ajax(实际上是 angularjs)调用来填充就地编辑选择字段时,类似的问题让我很烦恼。

我只想要一个 id 和 name 属性 to_json 并不断收到 MissingAttributeError。

意识到我自己被模型中的 as_json 方法困扰,该方法用于主索引并显示模型上的调用。基本上是 as_json 没有看到它期望的属性。

@foo=Foo.select("id,name")

respond_to do |format|
    format.json  { render :json => @foo.to_json }      
end

给出了错误,但

respond_to do |format|
    format.json  { render :json => { :foo=>@foo.as_json(:only=>[:id,:name]) } }     
end

似乎正在工作。我自己也差点怀疑这一点,但我在 找到了一个很好的解释。

http://jonathanjulian.com/2010/04/rails-to_json-or- as_json/

A similar problem was annoying me when I was trying to make Ajax (actually angularjs) calls to populate an edit-in-place select fields.

I just wanted an id and name attributes to_json and kept getting the MissingAttributeError.

Realised I gotcha'd myself by having an as_json method in the model which is used for the main index and show calls on the model. Basically it was the as_json that was not seeing the attributes it expected.

@foo=Foo.select("id,name")

respond_to do |format|
    format.json  { render :json => @foo.to_json }      
end

gave the error but

respond_to do |format|
    format.json  { render :json => { :foo=>@foo.as_json(:only=>[:id,:name]) } }     
end

seems to be working. I was close to sussing it myself but I found a great explanation at.

http://jonathanjulian.com/2010/04/rails-to_json-or-as_json/

清浅ˋ旧时光 2024-12-03 05:26:15

我通过将 .to_json 添加到控制器渲染的末尾来修复此问题。

I fixed this by adding .to_json to the end of my controller render.

浊酒尽余欢 2024-12-03 05:26:15

(不是特定于问题描述,而是特定于错误标题)

这可能对仍然看到“缺少属性”错误的人有帮助。

当我没有从数据库加载属性时,我遇到了这个错误。

user = User.where(some_criteria).only(:id, :first_name).first
user.last_name # throws missing attribute error because it wasn't loaded in `only` method

您可能只从某些存储中加载选择属性,只需确保您正在获取所需的字段即可。

(Not specific to the problem description but to the error title)

This may be helpful to someone who is still seeing the Missing Attribute error.

I faced this error when I wasn't loading the attribute from DB.

user = User.where(some_criteria).only(:id, :first_name).first
user.last_name # throws missing attribute error because it wasn't loaded in `only` method

You might be loading only the select attribute from some storage, just make sure you are fetching the required field.

月亮坠入山谷 2024-12-03 05:26:15

您需要

rescue ActiveRecord::MissingAttributeError 

在模型的 after_initialize() 方法中添加行

you need to add line

rescue ActiveRecord::MissingAttributeError 

in your after_initialize() method of the model

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文