Skip to content

Commit

Permalink
where.not adds references for includes.
Browse files Browse the repository at this point in the history
Closes #14406.

Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information
senny committed Mar 17, 2014
1 parent 3711112 commit b5abb4b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
* `where.not` adds `references` for `includes` like normal `where` calls do.

Fixes #14406.

*Yves Senn*

* Add support for `Relation` be passed as parameter on `QueryCache#select_all`.

Fixes #14361.
Expand Down
2 changes: 2 additions & 0 deletions activerecord/lib/active_record/relation/query_methods.rb
Expand Up @@ -49,6 +49,8 @@ def not(opts, *rest)
Arel::Nodes::Not.new(rel)
end
end

@scope.references!(PredicateBuilder.references(opts)) if Hash === opts
@scope.where_values += where_value
@scope
end
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/associations/eager_test.rb
Expand Up @@ -1203,4 +1203,14 @@ def test_deep_including_through_habtm
assert_equal 5, author.posts.size
}
end

test "including associations with where.not adds implicit references" do
author = assert_queries(2) {
Author.includes(:posts).where.not(posts: { title: 'Welcome to the weblog'} ).last
}

assert_no_queries {
assert_equal 2, author.posts.size
}
end
end
8 changes: 8 additions & 0 deletions activerecord/test/cases/relations_test.rb
Expand Up @@ -1342,6 +1342,14 @@ def test_automatically_added_where_references
assert_equal ['comments'], scope.references_values
end

def test_automatically_added_where_not_references
scope = Post.where.not(comments: { body: "Bla" })
assert_equal ['comments'], scope.references_values

scope = Post.where.not('comments.body' => 'Bla')
assert_equal ['comments'], scope.references_values
end

def test_automatically_added_having_references
scope = Post.having(:comments => { :body => "Bla" })
assert_equal ['comments'], scope.references_values
Expand Down

0 comments on commit b5abb4b

Please sign in to comment.