Sortable UUIDs and overriding ActiveRecord::Base

1.6k views Asked by At

I'm wanting to use UUIDs in an app I'm building and am running into a bit of a problem. Due to UUIDs (v4) not being sortable because they're randomly generated, I'm trying to override ActiveRecord::Base#first, but Rails isn't too pleased with that. It yells at me saying ArgumentError: You tried to define a scope named "first" on the model "Item", but Active Record already defined a class method with the same name. Do I have to use a different method if I want to sort and have it sort correctly?

Here's the sauce:

# lib/sortable_uuid.rb
module SortableUUID
  def self.included(base)
    base.class_eval do
      scope :first, -> { order("created_at").first }
      scope :last, -> { order("created_at DESC").first }
    end
  end
end


# app/models/item.rb
class Item < ActiveRecord::Base
  include SortableUUID
end

Rails 4.2, Ruby 2.2.2

Reference:

4

There are 4 answers

2
mu is too short On BEST ANSWER

First of all, first and last aren't as simple as you seem to think they are: you're completely neglecting the limit argument that both of those methods support.

Secondly, scope is little more than a fancy way of adding class methods that are intended to return queries. Your scopes are abusing scope because they return single model instances rather than queries. You don't want to use scope at all, you're just trying to replace the first and last class methods so why don't you just override them? You'd need to override them properly though and that will require reading and understanding the Rails source so that you properly mimic what find_nth_with_limit does. You'd want to override second, third, ... and the rest of those silly methods while you're at it.

If you don't feel right about replace first and last (a good thing IMO), then you could add a default scope to order things as desired:

default_scope -> { order(:created_at) }

Of course, default scopes come with their own set of problems and sneaking things into the ORDER BY like this will probably force you into calling reorder any time you actually want to specify the ORDER BY; remember that multiple calls to order add new ordering conditions, they don't replace one that's already there.

Alternatively, if you're using Rails6+, you can use Markus's implicit_order_column solution to avoid all the problems that default scopes can cause.


I think you're going about this all wrong. Any time I see M.first I assume that something has been forgotten. Ordering things by id is pretty much useless so you should always manually specify the order you want before using methods like first and last.

0
Alex On

The best way to do this according to the rails documentation linked here is to ensure you add the default option for your UUID you would like to use. A good write up on choosing a UUID linked here. I used ULIDs as they are sortable and with specifying my UUID option as the ULID, my records are sortable!

0
stevec On

After replacing id with uuid, I experienced some weirdness in the way associations were allocating foreign keys, and it wasn't that .last and .first, but instead because I simply forgot to add default: 'gen_random_uuid()' to one of the tables using a uuid. Once I fixed that, the problem was solved.

create_table :appointments, id: :uuid, default: 'gen_random_uuid()' do |t|
0
Markus On

Rails 6 (currently in version 6.0.0rc1) comes to rescue with implicit_order_column!

To order by created_at and make .first, .last, .second etc. respect it is as simple as:

class ApplicationRecord < ActiveRecord::Base
  self.implicit_order_column = :created_at
end