-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Association using unscope: NoMethodError: undefined method `children' for nil:NilClass #38811
Comments
Can you provide a reproduction script with this template https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_master.rb? |
@kamipo - Is this already fixed? Just run that test and I confirmed it works with ruby 2.6.3p62 Update: that test installed rails 6.1.0.alpha. Sorry. I'll try again. More update: still unable to reproduce it with this updated test: https://gist.github.com/kevinhq/3c5f9d000d6f3ddc648ba87d35cffc89. Ruby 2.6.3p62 and Rails 6.0.2.2 |
I also tested this issue but found that it does work perfectly with Ruby version It also works fine with recent Ruby version and Rails version. |
I'm getting the same error after upgrading to 5.2.4.2. Created a reproduction gist based on @kevinhq 's |
I tried to track this down and I think it relates to this line: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/query_methods.rb#L479 If A workaround I've found is to use the hash form |
Thanks @dvandersluis. Changing to the hash form fixed the tests that were failing because of this. There don't seem to be any problems with unscope when used inside a scope, just when it's used in an association definition. |
Confirmed this. Error happened on 5.2.4.2 with ruby 2.6.2p47 (2019-03-13 revision 67232) [x86_64-darwin19]
|
rails#29589 changed merging scope order to allow to unscope default scopes on association scope (rails#29611), but that caused a regression rails#38811 that accidentally allow join constraint which is required. ``` % bin/test test/cases/associations/has_many_associations_test.rb -n test_unscopes_the_default_scope_of_associated_model_when_used_with_include Run options: -n test_unscopes_the_default_scope_of_associated_model_when_used_with_include --seed 32978 # Running: E Error: HasManyAssociationsTest#test_unscopes_the_default_scope_of_associated_model_when_used_with_include: NoMethodError: undefined method `children' for nil:NilClass ~/rails/activerecord/lib/active_record/associations/join_dependency/join_association.rb:39:in `block in join_constraints' ~/rails/activerecord/lib/active_record/associations/join_dependency/join_association.rb:30:in `reverse_each' ~/rails/activerecord/lib/active_record/associations/join_dependency/join_association.rb:30:in `with_index' ~/rails/activerecord/lib/active_record/associations/join_dependency/join_association.rb:30:in `join_constraints' ~/rails/activerecord/lib/active_record/associations/join_dependency.rb:171:in `make_constraints' ~/rails/activerecord/lib/active_record/associations/join_dependency.rb:196:in `block in walk' ~/rails/activerecord/lib/active_record/associations/join_dependency.rb:196:in `each' ~/rails/activerecord/lib/active_record/associations/join_dependency.rb:196:in `flat_map' ~/rails/activerecord/lib/active_record/associations/join_dependency.rb:196:in `walk' ~/rails/activerecord/lib/active_record/associations/join_dependency.rb:90:in `block in join_constraints' ~/rails/activerecord/lib/active_record/associations/join_dependency.rb:87:in `each' ~/rails/activerecord/lib/active_record/associations/join_dependency.rb:87:in `flat_map' ~/rails/activerecord/lib/active_record/associations/join_dependency.rb:87:in `join_constraints' ~/rails/activerecord/lib/active_record/relation/query_methods.rb:1226:in `build_join_query' ~/rails/activerecord/lib/active_record/relation/query_methods.rb:1211:in `build_joins' ~/rails/activerecord/lib/active_record/relation/query_methods.rb:1091:in `build_arel' ~/rails/activerecord/lib/active_record/relation/query_methods.rb:1063:in `arel' ~/rails/activerecord/lib/active_record/relation/finder_methods.rb:419:in `block in limited_ids_for' ~/rails/activerecord/lib/active_record/relation.rb:867:in `skip_query_cache_if_necessary' ~/rails/activerecord/lib/active_record/relation/finder_methods.rb:419:in `limited_ids_for' ~/rails/activerecord/lib/active_record/relation/finder_methods.rb:398:in `apply_join_dependency' ~/rails/activerecord/lib/active_record/relation.rb:839:in `block in exec_queries' ~/rails/activerecord/lib/active_record/relation.rb:867:in `skip_query_cache_if_necessary' ~/rails/activerecord/lib/active_record/relation.rb:834:in `exec_queries' ~/rails/activerecord/lib/active_record/relation.rb:639:in `load' ~/rails/activerecord/lib/active_record/relation.rb:250:in `records' ~/rails/activerecord/lib/active_record/relation/finder_methods.rb:508:in `find_take' ~/rails/activerecord/lib/active_record/relation/finder_methods.rb:98:in `take' ~/rails/activerecord/lib/active_record/relation/finder_methods.rb:458:in `find_one' ~/rails/activerecord/lib/active_record/relation/finder_methods.rb:442:in `find_with_ids' ~/rails/activerecord/lib/active_record/relation/finder_methods.rb:69:in `find' ~/rails/activerecord/test/cases/associations/has_many_associations_test.rb:2689:in `block in <class:HasManyAssociationsTest>' bin/test test/cases/associations/has_many_associations_test.rb:2683 ``` Required join constraint should not be allowed to unscoping. Fixes rails#38811.
Steps to reproduce
Have the following models:
The following code will crash (even if the
default_scope
is removed):Expected behavior
Join tags to posts without the default scope being applied. Should generate a query something like:
This was the behaviour in Rails 5.2.3.
Actual behavior
System configuration
Rails version:
Works in 5.2.3, broken in 5.2.4.2 and 6.0.2.2.
Ruby version:
2.6.2
The text was updated successfully, but these errors were encountered: