Skip to content
Browse files

Raise ArgumentError when trying to merge relations of different classes

  • Loading branch information...
1 parent 92c982d commit b95cc72429f83304b8e882c3637dfb3135a571ed @lifo lifo committed
Showing with 7 additions and 0 deletions.
  1. +2 −0 activerecord/lib/active_record/relation.rb
  2. +5 −0 activerecord/test/cases/relations_test.rb
View
2 activerecord/lib/active_record/relation.rb
@@ -13,6 +13,8 @@ def initialize(klass, relation, readonly = false, preload = [], eager_load = [])
end
def merge(r)
+ raise ArgumentError, "Cannot merge a #{r.klass.name} relation with #{@klass.name} relation" if r.klass != @klass
+
joins(r.relation.joins(r.relation)).
group(r.send(:group_clauses).join(', ')).
order(r.send(:order_clauses).join(', ')).
View
5 activerecord/test/cases/relations_test.rb
@@ -353,4 +353,9 @@ def test_relation_merging_with_preload
assert_queries(2) { assert posts.first.author }
end
end
+
+ def test_invalid_merge
+ assert_raises(ArgumentError) { Post.scoped & Developer.scoped }
+ end
+
end

0 comments on commit b95cc72

Please sign in to comment.
Something went wrong with that request. Please try again.