1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Make the comparison between 'Relation' and 'AssociationRelation'

consistent.
This commit is contained in:
Lauro Caetano 2014-04-11 19:51:38 -03:00
parent d6840f914a
commit 24052f925f
3 changed files with 14 additions and 1 deletions

View file

@ -17,6 +17,10 @@ module ActiveRecord
@association.empty? @association.empty?
end end
def ==(other)
other == to_a
end
private private
def exec_queries def exec_queries

View file

@ -569,7 +569,7 @@ module ActiveRecord
# Compares two relations for equality. # Compares two relations for equality.
def ==(other) def ==(other)
case other case other
when Associations::CollectionProxy when Associations::CollectionProxy, AssociationRelation
self == other.to_a self == other.to_a
when Relation when Relation
other.to_sql == to_sql other.to_sql == to_sql

View file

@ -568,6 +568,15 @@ class BasicsTest < ActiveRecord::TestCase
assert Bulb.where(car_id: car.id) == car.bulbs.to_a, 'Relation should be comparable with Array' assert Bulb.where(car_id: car.id) == car.bulbs.to_a, 'Relation should be comparable with Array'
end 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 def test_hashing
assert_equal [ Topic.find(1) ], [ Topic.find(2).topic ] & [ Topic.find(1) ] assert_equal [ Topic.find(1) ], [ Topic.find(2).topic ] & [ Topic.find(1) ]
end end