Ruby-on-rails ActiveModel::MissingAttributeError 在部署后发生,一段时间后消失

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6820156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:39:31  来源:igfitidea点击:

ActiveModel::MissingAttributeError occurs after deploying and then goes away after a while

ruby-on-railsrubyactivemodel

提问by philnash

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.

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

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_zor when they are using cache_money. I am doing neither of these things.

我已经搜索了解决方案,但是这个错误主要发生在有人已经完成Model.all(:select => 'column_x,column_y')并正在调用column_z或当他们使用 cache_money 时。这些事情我都没有做。

Can anyone help?

任何人都可以帮忙吗?

回答by randomguy

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_initializecallback, which executes whenever a new ActiveRecord object is created (i.e. when fetched from the database).

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

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.

在那个初始化回调中,某些东西试图访问或使用未包含在:select. 您希望它为该属性返回 nil,但会抛出 ActiveRecord::MissingAttributeError 。

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.

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

回答by rmtsukuru

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

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

回答by scarver2

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

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

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 selectportion of your whereclause and see if the query/scope/method runs correctly. If so, then add all of the attributes to the selectand remove unneeded attributes one-at-a-time until you find the offending attribute.

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

回答by Robbie Guilfoyle

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

我通过添加.to_json到我的控制器渲染的末尾来解决这个问题。

回答by GSP

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.

我发现了一个有趣的问题,导致了同样的错误。为了重用代码,我们将一个 Presenters 类子类化为一个 Presenters 类,该类执行分组以在图形视图中使用。

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

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

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

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

回答by blythburgh

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

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

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

我只想要一个 id 和 name 属性 to_json 并不断收到 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.

意识到我通过在模型中使用 as_json 方法来解决自己的问题,该方法用于主索引并显示模型上的调用。基本上是 as_json 没有看到它预期的属性。

@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/

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

回答by Mukul Goyal

you need to add line

你需要添加行

rescue ActiveRecord::MissingAttributeError 

in your after_initialize() method of the model

在模型的 after_initialize() 方法中