File tree Expand file tree Collapse file tree 2 files changed +7
-1
lines changed Expand file tree Collapse file tree 2 files changed +7
-1
lines changed Original file line number Diff line number Diff line change @@ -2108,6 +2108,8 @@ def test_destroy_by
2108
2108
test "joins with order by custom attribute" do
2109
2109
companies = Company . create! ( [ { name : "test1" } , { name : "test2" } ] )
2110
2110
companies . each { |company | company . contracts . create! }
2111
+ # In ordering by Contract#metadata, we rely on that JSON string to
2112
+ # be consistent
2111
2113
assert_equal companies , Company . joins ( :contracts ) . order ( :metadata , :count )
2112
2114
assert_equal companies . reverse , Company . joins ( :contracts ) . order ( metadata : :desc , count : :desc )
2113
2115
end
Original file line number Diff line number Diff line change @@ -23,7 +23,11 @@ def bye
23
23
end
24
24
25
25
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 }
27
31
end
28
32
end
29
33
You can’t perform that action at this time.
0 commit comments