Skip to content

Commit 6577e8b

Browse files
committed
Revert "Merge pull request rails#47864 from zenspider/zenspider/ar_core_hash"
This reverts commit 2815cb9, reversing changes made to 11a5e37. The change broke application code and if we are going to make this change we will need to deprecate the prior behavior. See rails#47864 for more details.
1 parent 45b4917 commit 6577e8b

File tree

6 files changed

+23
-57
lines changed

6 files changed

+23
-57
lines changed

activerecord/lib/active_record/core.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -551,12 +551,10 @@ def encode_with(coder)
551551
# Note also that destroying a record preserves its ID in the model instance, so deleted
552552
# models are still comparable.
553553
def ==(comparison_object)
554-
return super if new_record?
555554
super ||
556555
comparison_object.instance_of?(self.class) &&
557556
primary_key_values_present? &&
558-
comparison_object.id == id &&
559-
!comparison_object.new_record?
557+
comparison_object.id == id
560558
end
561559
alias :eql? :==
562560

@@ -565,8 +563,8 @@ def ==(comparison_object)
565563
def hash
566564
id = self.id
567565

568-
if primary_key_values_present? && !new_record?
569-
[self.class, id].hash
566+
if primary_key_values_present?
567+
self.class.hash ^ id.hash
570568
else
571569
super
572570
end

activerecord/test/cases/attribute_methods_test.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -811,10 +811,8 @@ def topic.approved; false; end
811811

812812
test "YAML dumping a record with time zone-aware attribute" do
813813
in_time_zone "Pacific Time (US & Canada)" do
814-
Topic.where(id: 1).delete_all
815814
record = Topic.new(id: 1)
816815
record.written_on = "Jan 01 00:00:00 2014"
817-
record.save!
818816
payload = YAML.dump(record)
819817
assert_equal record, YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(payload) : YAML.load(payload)
820818
end

activerecord/test/cases/base_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,12 @@ def test_equality_of_destroyed_records
625625
assert_equal topic_2, topic_1
626626
end
627627

628+
def test_equality_with_blank_ids
629+
one = Subscriber.new(id: "")
630+
two = Subscriber.new(id: "")
631+
assert_equal one, two
632+
end
633+
628634
def test_equality_of_relation_and_collection_proxy
629635
car = Car.create!
630636
car.bulbs.build

activerecord/test/cases/core_test.rb

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,9 @@
77
require "models/cpk"
88

99
class NonExistentTable < ActiveRecord::Base; end
10-
class PkWithDefault < ActiveRecord::Base; end
1110

1211
class CoreTest < ActiveRecord::TestCase
13-
fixtures :topics, :cpk_books
14-
15-
def test_eql_on_default_pk
16-
saved_record = PkWithDefault.new
17-
saved_record.save!
18-
assert_equal 123, saved_record.id
19-
20-
record = PkWithDefault.new
21-
assert_equal 123, record.id
22-
23-
record2 = PkWithDefault.new
24-
assert_equal 123, record2.id
25-
26-
assert record.eql?(record), "record should eql? itself"
27-
assert_not record.eql?(saved_record), "new record should not eql? saved"
28-
assert_not saved_record.eql?(record), "saved record should not eql? new"
29-
assert_not record.eql?(record2), "new record should not eql? new record"
30-
assert_not record2.eql?(record), "new record should not eql? new record"
31-
end
12+
fixtures :topics
3213

3314
def test_inspect_class
3415
assert_equal "ActiveRecord::Base", ActiveRecord::Base.inspect
@@ -174,12 +155,12 @@ def test_find_by_cache_does_not_duplicate_entries
174155

175156
def test_composite_pk_models_added_to_a_set
176157
library = Set.new
177-
# new record with primary key present
158+
# with primary key present
178159
library << Cpk::Book.new(author_id: 1, number: 2)
179160

180161
# duplicate
181-
library << cpk_books(:cpk_great_author_first_book)
182-
library << cpk_books(:cpk_great_author_first_book)
162+
library << Cpk::Book.new(author_id: 1, number: 3)
163+
library << Cpk::Book.new(author_id: 1, number: 3)
183164

184165
# without primary key being set
185166
library << Cpk::Book.new(title: "Book A")
@@ -189,34 +170,22 @@ def test_composite_pk_models_added_to_a_set
189170
end
190171

191172
def test_composite_pk_models_equality
192-
book = cpk_books(:cpk_great_author_first_book)
193-
book_instance_1 = Cpk::Book.find_by(author_id: book.author_id, number: book.number)
194-
book_instance_2 = Cpk::Book.find_by(author_id: book.author_id, number: book.number)
195-
196-
assert book_instance_1 == book_instance_1
197-
assert book_instance_1 == book_instance_2
173+
assert Cpk::Book.new(author_id: 1, number: 2) == Cpk::Book.new(author_id: 1, number: 2)
198174

199-
# two new records with the same primary key
200-
assert_not Cpk::Book.new(author_id: 1, number: 2) == Cpk::Book.new(author_id: 1, number: 2)
201-
# two new records with an empty primary key values
175+
assert_not Cpk::Book.new(author_id: 1, number: 2) == Cpk::Book.new(author_id: 1, number: 3)
202176
assert_not Cpk::Book.new == Cpk::Book.new
203-
# two persisted records with a different primary key
204-
assert_not cpk_books(:cpk_great_author_first_book) == cpk_books(:cpk_great_author_second_book)
177+
assert_not Cpk::Book.new(title: "Book A") == Cpk::Book.new(title: "Book B")
178+
assert_not Cpk::Book.new(author_id: 1) == Cpk::Book.new(author_id: 1)
179+
assert_not Cpk::Book.new(author_id: 1, title: "Same title") == Cpk::Book.new(author_id: 1, title: "Same title")
205180
end
206181

207182
def test_composite_pk_models_hash
208-
book = cpk_books(:cpk_great_author_first_book)
209-
book_instance_1 = Cpk::Book.find_by(author_id: book.author_id, number: book.number)
210-
book_instance_2 = Cpk::Book.find_by(author_id: book.author_id, number: book.number)
211-
212-
assert_equal book_instance_1.hash, book_instance_1.hash
213-
assert_equal book_instance_1.hash, book_instance_2.hash
183+
assert_equal Cpk::Book.new(author_id: 1, number: 2).hash, Cpk::Book.new(author_id: 1, number: 2).hash
214184

215-
# two new records with the same primary key
216-
assert_not_equal Cpk::Book.new(author_id: 1, number: 2).hash, Cpk::Book.new(author_id: 1, number: 2).hash
217-
# two new records with an empty primary key values
185+
assert_not_equal Cpk::Book.new(author_id: 1, number: 2).hash, Cpk::Book.new(author_id: 1, number: 3).hash
218186
assert_not_equal Cpk::Book.new.hash, Cpk::Book.new.hash
219-
# two persisted records with a different primary key
220-
assert_not_equal cpk_books(:cpk_great_author_first_book).hash, cpk_books(:cpk_great_author_second_book).hash
187+
assert_not_equal Cpk::Book.new(title: "Book A").hash, Cpk::Book.new(title: "Book B").hash
188+
assert_not_equal Cpk::Book.new(author_id: 1).hash, Cpk::Book.new(author_id: 1).hash
189+
assert_not_equal Cpk::Book.new(author_id: 1, title: "Same title").hash, Cpk::Book.new(author_id: 1, title: "Same title").hash
221190
end
222191
end

activerecord/test/cases/filter_attributes_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ class FilterAttributesTest < ActiveRecord::TestCase
6868
ActiveRecord::Base.filter_attributes = [ lambda { |key, value| value.reverse! if key == "name" } ]
6969
account = Admin::Account.new(id: 123, name: "37signals")
7070
account.inspect
71-
account.save!
7271
assert_equal account, Marshal.load(Marshal.dump(account))
7372
end
7473

activerecord/test/schema/schema.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,10 +1411,6 @@
14111411
t.bigint :toooooooo_long_a_id, null: false
14121412
t.bigint :toooooooo_long_b_id, null: false
14131413
end
1414-
1415-
create_table :pk_with_defaults, id: false, force: true do |t|
1416-
t.bigint :id, default: 123
1417-
end
14181414
end
14191415

14201416
Course.connection.create_table :courses, force: true do |t|

0 commit comments

Comments
 (0)