Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #14592 from laurocaetano/equality_between_relation…

…_and_collection_proxy

The comparison between `Relation` and `CollectionProxy` should be consistent.

Conflicts:
	activerecord/CHANGELOG.md

Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information...
commit 6c61893ceda2fb8807eb9a4b30ee65f4ff4af56a 1 parent 461171b
@rafaelfranca rafaelfranca authored
View
13 activerecord/CHANGELOG.md
@@ -1,3 +1,16 @@
+* The comparison between `Relation` and `CollectionProxy` should be consistent.
+
+ Example:
+
+ author.posts == Post.where(author_id: author.id)
+ # => true
+ Post.where(author_id: author.id) == author.posts
+ # => true
+
+ Fixes #13506.
+
+ *Lauro Caetano*
+
* PostgreSQL adapter only warns once for every missing OID per connection.
Fixes #14275.
View
4 activerecord/lib/active_record/association_relation.rb
@@ -17,6 +17,10 @@ def empty?
@association.empty?
end
+ def ==(other)
+ other == to_a
+ end
+
private
def exec_queries
View
2  activerecord/lib/active_record/relation.rb
@@ -569,6 +569,8 @@ def uniq_value
# Compares two relations for equality.
def ==(other)
case other
+ when Associations::CollectionProxy, AssociationRelation
+ self == other.to_a
when Relation
other.to_sql == to_sql
when Array
View
35 activerecord/test/cases/base_test.rb
@@ -551,6 +551,41 @@ def test_equality_with_blank_ids
assert_equal one, two
end
+ def test_equality_of_relation_and_collection_proxy
+ car = Car.create!
+ car.bulbs.build
+ car.save
+
+ assert car.bulbs == Bulb.where(car_id: car.id), 'CollectionProxy should be comparable with Relation'
+ assert Bulb.where(car_id: car.id) == car.bulbs, 'Relation should be comparable with CollectionProxy'
+ end
+
+ def test_equality_of_relation_and_array
+ car = Car.create!
+ car.bulbs.build
+ car.save
+
+ 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_equality_of_collection_proxy_and_association_relation
+ car = Car.create!
+ car.bulbs.build
+ car.save
+
+ assert_equal car.bulbs, car.bulbs.includes(:car), 'CollectionProxy should be comparable with AssociationRelation'
+ assert_equal car.bulbs.includes(:car), car.bulbs, 'AssociationRelation should be comparable with CollectionProxy'
+ end
+
def test_hashing
assert_equal [ Topic.find(1) ], [ Topic.find(2).topic ] & [ Topic.find(1) ]
end
Please sign in to comment.
Something went wrong with that request. Please try again.