aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2014-03-17 14:20:23 +0100
committerYves Senn <yves.senn@gmail.com>2014-03-17 14:20:23 +0100
commit8ba60912ca42a66cb59a31346623a6f9d2439192 (patch)
treeff1a786634dad3141a8084b8a224dd47b529eaac
parent378c8d2c996558aa4108280d5f0db8daf040d0fc (diff)
downloadrails-8ba60912ca42a66cb59a31346623a6f9d2439192.tar.gz
rails-8ba60912ca42a66cb59a31346623a6f9d2439192.tar.bz2
rails-8ba60912ca42a66cb59a31346623a6f9d2439192.zip
`where.not` adds `references` for `includes`.
Closes #14406.
-rw-r--r--activerecord/CHANGELOG.md6
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb2
-rw-r--r--activerecord/test/cases/associations/eager_test.rb10
-rw-r--r--activerecord/test/cases/relations_test.rb8
4 files changed, 26 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 4018b3fd84..b648eed06a 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
+* `where.not` adds `references` for `includes` like normal `where` calls do.
+
+ Fixes #14406.
+
+ *Yves Senn*
+
* Extend fixture `$LABEL` replacement to allow string interpolation.
Example:
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 8c005a7222..e41df0ea29 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -49,6 +49,8 @@ module ActiveRecord
Arel::Nodes::Not.new(rel)
end
end
+
+ @scope.references!(PredicateBuilder.references(opts)) if Hash === opts
@scope.where_values += where_value
@scope
end
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index 416a39ea4c..e59161fc5b 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -1203,4 +1203,14 @@ class EagerAssociationTest < ActiveRecord::TestCase
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
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 3b4b4c92f0..fddb7c204a 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -1366,6 +1366,14 @@ class RelationTest < ActiveRecord::TestCase
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