ActiveRecord::Base#hash should differ between classes

Prior to this change, we would get collisions if Active Record objects
of different classes with the same ID were used as keys of the same
hash. It bothers me slightly that we have to allocate inside of this
method, but Ruby doesn't provide any way to hash multiple values without
allocation
This commit is contained in:
Sean Griffin 2016-05-31 11:35:46 -04:00
parent 81251c6d99
commit c8be4574a2
2 changed files with 5 additions and 1 deletions

@ -432,7 +432,7 @@ def ==(comparison_object)
# [ Person.find(1), Person.find(2), Person.find(3) ] & [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]
def hash
if id
id.hash
[self.class, id].hash
else
super
end

@ -1504,6 +1504,10 @@ def test_default_values_are_deeply_dupped
assert_not_equal Post.new.hash, Post.new.hash
end
test "records of different classes have different hashes" do
assert_not_equal Post.new(id: 1).hash, Developer.new(id: 1).hash
end
test "resetting column information doesn't remove attribute methods" do
topic = topics(:first)