image by Jeff Frenette
Using UUIDs as primary keys has many benefits. However, it causes issues with Rails’s implicit ordering.
In a previous article, I suggested using named scopes alongside
last, but there is now an easier way to re-enable the default behaviour of Active Record.
#last when using non-sequential
ids on an Active Record model, or using the extra specificity provided by a named scope…
implicit_order_column within your model and aim it at the automatically generated
class Coffee < ApplicationRecord self.implicit_order_column = "created_at" end
There are major benefits to using UUIDs in your database: uniqueness, assignability, and security.
With this one-line change you also retain the benefits of Rails’
.last helper methods, provided you assign the
implicit_order_column to the
This is a Rails 6 feature. Prior versions of Rails still require the explicit ordering implementation previously explained.
You may still prefer using the explicit ordering approach for greater clarity.
If you do not use UUIDs in your data model, there’s little point in using this.
Note that if two records are created at exactly the same time, the order of the results for those records will be based on the order of their primary key.
Another issue is that, by default,
created_at does not have a database index, unlike the
id primary key. Running the query without the index could take a while if the dataset is very large. You should add an database index on the
Benjamin Alexander for making the point about needing a database index on
Last updated on March 1st, 2020 by @andycroll
An email newsletter, with one Ruby/Rails technique delivered with a ‘why?’ and a ‘how?’ every two weeks. It’s deliberately brief, focussed & opinionated.