From 74514b462dcd846121f690ebb77ff8f0be4ae353 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Wed, 13 Nov 2024 20:31:25 -0600 Subject: [PATCH] Add a caveat to the latest TIL --- rails/empty-find-by-returns-first-record.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rails/empty-find-by-returns-first-record.md b/rails/empty-find-by-returns-first-record.md index 39f0f3a..7daff83 100644 --- a/rails/empty-find-by-returns-first-record.md +++ b/rails/empty-find-by-returns-first-record.md @@ -41,3 +41,9 @@ the query. And the `#take` corresponds to the `limit 1`. Knowing that, we can understand that we will also get the first record from the database if we call `#find_by` with `{}`. Again, no conditions to filter on, so give me all books limited to one. + +One small caveat: notice how there is no `order by` clause in the above SQL +output. This differs from `Books.first` which implicitly does an order on the +`id` column. Though these method are likely to return the same result, the +ordering of `#find_by` is not guaranteed to be the same without an `order by` +clause.