Skip to content

Commit

Permalink
Ensure none constraint scopes don’t trigger a query
Browse files Browse the repository at this point in the history
#47800 refactored how `Relation#none` worked, and one side effect of this change was that if you had an association like this:

`has_many :comments_none, ->(post) { none }, class_name: "Comment”`

reading the association caused a query to happen, despite the `none` in the passed in scope.

As it turns out, this is because the association performs a merge of the two scopes, and the `@none` instance variable wasn’t getting copied over to the merged scope, so the upfront guard clause checking `@none` in `#exec_main_query`.

Updating the `Merger` object to add a check for null relations and call `none!` ensures the query doesn’t run in this particular scenario.
  • Loading branch information
aharpole committed Apr 11, 2023
1 parent 2675c90 commit c210600
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions activerecord/lib/active_record/relation/merger.rb
Expand Up @@ -69,6 +69,8 @@ def merge
end
end

relation.none! if other.null_relation?

merge_select_values
merge_multi_values
merge_single_values
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/null_relation_test.rb
Expand Up @@ -59,6 +59,16 @@ def test_null_relation_content_size_methods
end
end

def test_null_relation_used_with_constraints
post = Post.first
assert_no_queries do
scope = post.comments
none = Post.none
scope = scope.merge(none)
assert_equal 0, scope.size
end
end

def test_null_relation_metadata_methods
assert_includes Developer.none.to_sql, " WHERE (1=0)"
assert_equal({}, Developer.none.where_values_hash)
Expand Down

0 comments on commit c210600

Please sign in to comment.