diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index c80f573f01..110cfc6d0b 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -1513,8 +1513,11 @@ module ActiveRecord values = other.values STRUCTURAL_OR_METHODS.reject do |method| v1, v2 = @values[method], values[method] - v1 = v1.uniq if v1.is_a?(Array) - v2 = v2.uniq if v2.is_a?(Array) + if v1.is_a?(Array) + next true unless v2.is_a?(Array) + v1 = v1.uniq + v2 = v2.uniq + end v1 == v2 || (!v1 || v1.empty?) && (!v2 || v2.empty?) end end diff --git a/activerecord/test/cases/relation/merging_test.rb b/activerecord/test/cases/relation/merging_test.rb index d7f6b43cec..0da5dd7cbc 100644 --- a/activerecord/test/cases/relation/merging_test.rb +++ b/activerecord/test/cases/relation/merging_test.rb @@ -43,7 +43,7 @@ class RelationMergingTest < ActiveRecord::TestCase assert_equal [mary], david_and_mary.and(mary_and_bob) assert_equal authors, david_and_mary.or(mary_and_bob) - david_and_bob = Author.where(id: david).or(Author.where(name: "Bob")).order(:id) + david_and_bob = Author.where(id: david).or(Author.where(name: "Bob")) assert_equal [david], david_and_mary.merge(david_and_bob) assert_equal [david], david_and_mary.merge(david_and_bob, rewhere: true) @@ -96,7 +96,7 @@ class RelationMergingTest < ActiveRecord::TestCase assert_equal [mary], david_and_mary.and(mary_and_bob) assert_equal authors, david_and_mary.or(mary_and_bob) - david_and_bob = Author.where(id: david).or(Author.where(name: "Bob")).order(:id) + david_and_bob = Author.where(id: david).or(Author.where(name: "Bob")) assert_equal [david], david_and_mary.merge(david_and_bob) assert_equal [david], david_and_mary.merge(david_and_bob, rewhere: true) @@ -149,7 +149,7 @@ class RelationMergingTest < ActiveRecord::TestCase assert_equal [mary], david_and_mary.and(mary_and_bob) assert_equal authors, david_and_mary.or(mary_and_bob) - david_and_bob = Author.where(id: david).or(Author.where(name: "Bob")).order(:id) + david_and_bob = Author.where(id: david).or(Author.where(name: "Bob")) assert_equal [david], david_and_mary.merge(david_and_bob) assert_equal [david], david_and_mary.merge(david_and_bob, rewhere: true) diff --git a/activerecord/test/cases/relation/or_test.rb b/activerecord/test/cases/relation/or_test.rb index 4e6a78e283..b3dbbc4d6d 100644 --- a/activerecord/test/cases/relation/or_test.rb +++ b/activerecord/test/cases/relation/or_test.rb @@ -84,11 +84,12 @@ module ActiveRecord def test_or_with_unscope_order expected = Post.where("id = 1 or id = 2") assert_equal expected, Post.order("body asc").where("id = 1").unscope(:order).or(Post.where("id = 2")).to_a + assert_equal expected, Post.order(:id).where("id = 1").or(Post.order(:id).where("id = 2").unscope(:order)).to_a end def test_or_with_incompatible_unscope error = assert_raises ArgumentError do - Post.order("body asc").where("id = 1").or(Post.order("body asc").where("id = 2").unscope(:order)).to_a + Post.order("body asc").where("id = 1").unscope(:order).or(Post.order("body asc").where("id = 2")).to_a end assert_equal "Relation passed to #or must be structurally compatible. Incompatible values: [:order]", error.message