Skip to content

Commit 8d04150

Browse files
committed
Ensure Contract#metadata is reliably orderable
This gets an annoyingly verbose comment, because otherwise it could be removed and continue passing almost all the time.
1 parent 26caacb commit 8d04150

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

activerecord/test/cases/relations_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,6 +2108,8 @@ def test_destroy_by
21082108
test "joins with order by custom attribute" do
21092109
companies = Company.create!([{ name: "test1" }, { name: "test2" }])
21102110
companies.each { |company| company.contracts.create! }
2111+
# In ordering by Contract#metadata, we rely on that JSON string to
2112+
# be consistent
21112113
assert_equal companies, Company.joins(:contracts).order(:metadata, :count)
21122114
assert_equal companies.reverse, Company.joins(:contracts).order(metadata: :desc, count: :desc)
21132115
end

activerecord/test/models/contract.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ def bye
2323
end
2424

2525
def update_metadata
26-
self.metadata = { company_id: company_id, developer_id: developer_id }
26+
# 'code' makes the JSON string consistently orderable, which is used
27+
# by RelationsTest "joins with order by custom attribute". Without
28+
# this it would still pass 99% of the time, but fail when two
29+
# records' company_id lexical and numeric order differ (99, 100).
30+
self.metadata = { code: company_id && "%08x" % company_id, company_id: company_id, developer_id: developer_id }
2731
end
2832
end
2933

0 commit comments

Comments
 (0)