Merge pull request #41465 from ashiksp/active_record_query_without

Added ActiveRecord::Relation#without as alias for #excluding.
This commit is contained in:
Kasper Timm Hansen 2021-02-16 21:58:43 +01:00 committed by GitHub
commit 5b988afda4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 1 deletions

View File

@ -17,7 +17,7 @@ module ActiveRecord
:and, :or, :annotate, :optimizer_hints, :extending,
:having, :create_with, :distinct, :references, :none, :unscope, :merge, :except, :only,
:count, :average, :minimum, :maximum, :sum, :calculate,
:pluck, :pick, :ids, :strict_loading, :excluding
:pluck, :pick, :ids, :strict_loading, :excluding, :without
].freeze # :nodoc:
delegate(*QUERYING_METHODS, to: :all)

View File

@ -1140,6 +1140,11 @@ module ActiveRecord
spawn.excluding!(records)
end
# Alias for #excluding.
def without(*records)
excluding(*records)
end
def excluding!(records) # :nodoc:
# Treat single and multiple records differently in order to keep query
# clean in case of single record, ie, use != operator instead of NOT IN ().

View File

@ -68,4 +68,10 @@ class ExcludingTest < ActiveRecord::TestCase
end
assert_equal "You must only pass a single or collection of Post objects to #excluding.", exception.message
end
def test_result_set_does_not_include_without_record
post = posts(:welcome)
assert_not_includes Post.without(post).to_a, post
end
end