Skip to content
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

Don't ignore joins from association scopes in preloader #12538

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
* Merge joins from association scopes when using .includes() or .joins()

Fixes #12492.

*Mike Campbell*

* Prevent the inversed association from being reloaded on save.

Fixes #9499.
Expand Down
Expand Up @@ -118,6 +118,7 @@ def build_scope

scope.select! preload_values[:select] || values[:select] || table[Arel.star]
scope.includes! preload_values[:includes] || values[:includes]
scope.joins! preload_values[:joins] || values[:joins]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to either splat * the argument or change joins! in query_methods.rb to not use +=


if preload_values.key? :order
scope.order! preload_values[:order]
Expand Down
3 changes: 3 additions & 0 deletions activerecord/lib/active_record/relation/query_methods.rb
Expand Up @@ -400,6 +400,9 @@ def joins(*args)
end

def joins!(*args) # :nodoc:
args.reject!(&:blank?)
args.flatten!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change needed for this PR. I think this should be extracted to separated PR. What do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, when there was no join conditions (preload_values[:joins] || values[:joins] == nil) it was causing an error, 250 or so tests were failing. I noticed that .includes! has this code in it, and that was why it wasn't failing with no includes conditions, didn't see why the two weren't the same.

I could've put this check in associations/preloader/association.rb#build_scope instead with something like:

joins = preload_values[:joins] || values[:joins]
scope.joins! joins if joins

(or something cleaner, perhaps modifying joins_values directly as it does with where_values etc) but changing joins! to match includes! felt a nicer solution to me and didn't break any existing tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarification, agree to leave this as is.


self.joins_values += args
self
end
Expand Down