Scope Sequences in ActiveRecord
Recently I have been working on Helpful which is a support tool being built through the Assembly platform.
Internally we use UUIDs for everything but for user facing interactions we wanted nice integer id numbers. So what we want is an auto incrementing unique integer column. Low and behold this doesn't exist in Postgres as far as I can tell. I took this opportunity to do something I have been itching to do, create a gem.
Introducing Sequential. Sequential is a thread safe ActiveRecord plugin which gives you a really easy to use auto incrementing unique integer column
Here is an example Rails mode for a widget:
class Widget < ActiveRecord::Base
include ActiveRecord::UUID
belongs_to :account
end
Let's add an auto incrementing unique integer column to Widget that is scoped on :account_id
:
class Widget < ActiveRecord::Base
include ActiveRecord::UUID
belongs_to :account
sequential scope: :account_id
end
All you have to do now is add a migration to create the column. By default the column is called sequential_id but we can override that:
sequential column: :number, scope: :account_id
That's it, nice and simple. Check it out: https://github.com/austinylin/sequential or gem install sequential
Many thanks to Pugio for his help in creating Sequential.