DEV Community

Augusts Bautra
Augusts Bautra

Posted on

4

TIL custom order with .in_order_of

Sometimes you need a custom, semantic order for things, usually statuses, types. Oftentimes this is achieved with an SQL CASE statement:

sql = <<~SQL
  CASE
    WHEN status = 'active' THEN 0
    WHEN status = 'draft'  THEN 1    
    ELSE 99
  END
SQL 

order(sql, :id)
Enter fullscreen mode Exit fullscreen mode

Since at least Rails 7.1 there's a better way - in_order_of!

in_order_of(:status, [:active, :draft], filter: false).order(:id)
Enter fullscreen mode Exit fullscreen mode

Interestingly, v7.1 guide does not list this method at all, but it's available in edge guide.

Small caveat emptor - the filter: false option does not seem to be available in v7.1 yet.

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more