提交 d6840f91 编写于 作者: L Lauro Caetano

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
上级 c8a70660
* 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.
......
......@@ -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
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册