Browse files

The comparison between `Relation` and `CollectionProxy` should be con…

…sistent.

Example:

    author.posts == Post.where(author_id: author.id)
    # => true
      Post.where(author_id: author.id) == author.posts
    # => true

Fixes #13506
  • Loading branch information...
1 parent c8a7066 commit d6840f914a32bff4d73f23c3f5c64c5397f8b400 @laurocaetano laurocaetano committed Apr 3, 2014
Showing with 32 additions and 0 deletions.
  1. +13 −0 activerecord/CHANGELOG.md
  2. +2 −0 activerecord/lib/active_record/relation.rb
  3. +17 −0 activerecord/test/cases/base_test.rb
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*
+
* Fixed error for aggregate methods (`empty?`, `any?`, `count`) with `select`
which created invalid SQL.
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
+ self == other.to_a
when Relation
other.to_sql == to_sql
when Array
View
17 activerecord/test/cases/base_test.rb
@@ -551,6 +551,23 @@ 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_hashing
assert_equal [ Topic.find(1) ], [ Topic.find(2).topic ] & [ Topic.find(1) ]
end

0 comments on commit d6840f9

Please sign in to comment.