Skip to content

Files

Latest commit

 

History

History
24 lines (15 loc) · 720 Bytes

Rails-OrderById.md

File metadata and controls

24 lines (15 loc) · 720 Bytes

Pattern: Use of id column for ordering

Issue: -

Description

This rule checks for places where ordering by id column is used.

Don’t use the id column for ordering. The sequence of ids is not guaranteed to be in any particular order, despite often (incidentally) being chronological. Use a timestamp column to order chronologically. As a bonus the intent is clearer.

Examples

# bad
scope :chronological, -> { order(id: :asc) }
scope :chronological, -> { order(primary_key => :asc) }

# good
scope :chronological, -> { order(created_at: :asc) }

Further Reading