Permalink
Browse files

Make the comparison between 'Relation' and 'AssociationRelation'

consistent.
  • Loading branch information...
1 parent d6840f9 commit 24052f925f5f52f74646610a843eb3b98845ac77 @laurocaetano laurocaetano committed Apr 11, 2014
@@ -17,6 +17,10 @@ def empty?
@association.empty?
end
+ def ==(other)
+ other == to_a
+ end
+
private
def exec_queries
@@ -569,7 +569,7 @@ def uniq_value
# Compares two relations for equality.
def ==(other)
case other
- when Associations::CollectionProxy
+ when Associations::CollectionProxy, AssociationRelation
self == other.to_a
when Relation
other.to_sql == to_sql
@@ -568,6 +568,15 @@ def test_equality_of_relation_and_array
assert Bulb.where(car_id: car.id) == car.bulbs.to_a, 'Relation should be comparable with Array'
end
+ def test_equality_of_relation_and_association_relation
+ car = Car.create!
+ car.bulbs.build
+ car.save
+
+ assert_equal Bulb.where(car_id: car.id), car.bulbs.includes(:car), 'Relation should be comparable with AssociationRelation'
+ assert_equal car.bulbs.includes(:car), Bulb.where(car_id: car.id), 'AssociationRelation should be comparable with Relation'
+ end
+
def test_hashing
assert_equal [ Topic.find(1) ], [ Topic.find(2).topic ] & [ Topic.find(1) ]
end

0 comments on commit 24052f9

Please sign in to comment.