简体   繁体   中英

invalid argument: nil error after upgrading to Rails 5

I upgraded to Rails 5 and after starting the server the login forms must be loaded. Then this error occurs:

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'

That seems to come from Devise I think? After searching I found someone with the same error:

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)

What should I do?

nil is no longer valid as merge parameter, since that method expect a parameter of class ActiveRecord_Relation .

This issue is related to this PR on Rails . This is the resolution commit on rails.

To avoid this error:

  • check merge attribute before calling it on a relation, and append it only if it's not null.
  • or pass always a valid ActiveRecord_Relation to merge , for example <ARModel>.all instead of nil if the actual parameter is nil.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM