无效参数:升级到Rails 5后出现nil错误

时间:2016-11-02 09:06:39

标签: ruby-on-rails ruby-on-rails-5

我升级到Rails 5,启动服务器后必须加载登录表单。然后发生此错误:

ArgumentError - invalid argument: nil.:
  activerecord (5.0.0.1) lib/active_record/relation/spawn_methods.rb:36:in `merge'
  activerecord (5.0.0.1) lib/active_record/scoping/default.rb:119:in `block (2 levels) in build_default_scope'
  activerecord (5.0.0.1) lib/active_record/scoping/default.rb:117:in `block in build_default_scope'
  activerecord (5.0.0.1) lib/active_record/scoping/default.rb:141:in `evaluate_default_scope'
  activerecord (5.0.0.1) lib/active_record/scoping/default.rb:116:in `build_default_scope'
  activerecord (5.0.0.1) lib/active_record/scoping/named.rb:33:in `default_scoped'
  activerecord (5.0.0.1) lib/active_record/scoping/named.rb:28:in `all'
  activerecord (5.0.0.1) lib/active_record/scoping.rb:24:in `scope_attributes'
  activerecord (5.0.0.1) lib/active_record/scoping.rb:36:in `populate_with_current_scope_attributes'
  activerecord (5.0.0.1) lib/active_record/scoping.rb:43:in `initialize_internals_callback'
  activerecord (5.0.0.1) lib/active_record/core.rb:317:in `initialize'
  activerecord (5.0.0.1) lib/active_record/inheritance.rb:65:in `new'
  devise (4.2.0) app/controllers/devise/sessions_controller.rb:9:in `new'

这似乎来自Devise我想? 搜索后我发现有人犯了同样的错误:

Turns it it was a gem which behind the scenes was attempting to do default_scope { nil }, looks like someone implemented protection against that (since it shouldn't really work)

我该怎么办?

1 个答案:

答案 0 :(得分:0)

nil不再作为merge参数有效,因为该方法需要一个ActiveRecord_Relation类的参数。

此问题与this PR on Rails有关。 这是resolution commit

为避免此错误:

    在对关系进行调用之前,先
  • 检查merge属性,并仅在它不为null时附加它。
  • 始终将有效的ActiveRecord_Relation传递给merge,例如,<ARModel>.all而不是nil(如果实际参数为nil)。