【问题标题】:invalid argument: nil error after upgrading to Rails 5无效参数:升级到 Rails 5 后出现 nil 错误
【发布时间】:2016-11-02 09:06:39
【问题描述】:

我升级到 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'

我认为这似乎来自设计? 搜索后我发现有人有同样的错误:

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)

我该怎么办?

【问题讨论】:

  • 愚蠢的问题:你需要更新到 Rails 5 吗?您是否尝试更新设计 gem?
  • 是的,我也升级了 Devise

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


【解决方案1】:

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

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

为避免此错误:

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

【讨论】:

    猜你喜欢
    • 2020-08-25
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 2020-12-28
    • 2013-09-07
    • 2020-01-19
    相关资源
    最近更新 更多