Skip to content

Add ActiveRecord::Relation#structurally_compatible?. #41841

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

Merged
merged 2 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
* Add `ActiveRecord::Relation#structurally_compatible?`.

Adds a query method by which a user can tell if the relation that they're
about to use for `#or` or `#and` is structurally compatible with the
receiver.

*Kevin Newton*

* Add `ActiveRecord::QueryMethods#in_order_of`.

This allows you to specify an explicit order that you'd like records
Expand Down
15 changes: 15 additions & 0 deletions activerecord/lib/active_record/relation/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,21 @@ def invert_where! # :nodoc:
self
end

# Checks whether the given relation is structurally compatible with this relation, to determine
# if it's possible to use the #and and #or methods without raising an error. Structurally
# compatible is defined as: they must be scoping the same model, and they must differ only by
# #where (if no #group has been defined) or #having (if a #group is present).
#
# Post.where("id = 1").structurally_compatible?(Post.where("author_id = 3"))
# # => true
#
# Post.joins(:comments).structurally_compatible?(Post.where("id = 1"))
# # => false
#
def structurally_compatible?(other)
structurally_incompatible_values_for(other).empty?
end

# Returns a new relation, which is the logical intersection of this relation and the one passed
# as an argument.
#
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/relation/delegation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class QueryingMethodsDelegationTest < ActiveRecord::TestCase
ActiveRecord::FinderMethods.public_instance_methods(false) - [:include?, :member?, :raise_record_not_found_exception!] +
ActiveRecord::SpawnMethods.public_instance_methods(false) - [:spawn, :merge!] +
ActiveRecord::QueryMethods.public_instance_methods(false).reject { |method|
method.end_with?("=", "!", "value", "values", "clause")
method.end_with?("=", "!", "?", "value", "values", "clause")
} - [:reverse_order, :arel, :extensions, :construct_join_dependency] + [
:any?, :many?, :none?, :one?,
:first_or_create, :first_or_create!, :first_or_initialize,
Expand Down
38 changes: 38 additions & 0 deletions activerecord/test/cases/relation/structural_compatibility_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require "cases/helper"
require "models/post"

module ActiveRecord
class StructuralCompatibilityTest < ActiveRecord::TestCase
fixtures :posts

def test_compatible_values
left = Post.where(id: 1)
right = Post.where(id: 2)

assert left.structurally_compatible?(right)
end

def test_incompatible_single_value_relations
left = Post.distinct.where("id = 1")
right = Post.where(id: [2, 3])

assert_not left.structurally_compatible?(right)
end

def test_incompatible_multi_value_relations
left = Post.order("body asc").where("id = 1")
right = Post.order("id desc").where(id: [2, 3])

assert_not left.structurally_compatible?(right)
end

def test_incompatible_unscope
left = Post.order("body asc").where("id = 1").unscope(:order)
right = Post.order("body asc").where("id = 2")

assert_not left.structurally_compatible?(right)
end
end
end