---
title: Order by timestamp columns, not id
impact: MEDIUM
impactDescription: Ensure consistent ordering across database systems.
tags: database, rails, active-record, ordering
---

## Order by timestamp columns, not id

Ensure consistent ordering across database systems. Order records chronologically by timestamp columns (`created_at`, `updated_at`) instead of `id`. IDs may not be sequential in distributed systems (e.g., UUIDs).

**Incorrect:**

```ruby
scope :recent, -> { order(id: :desc) }
```

**Correct:**

```ruby
scope :recent, -> { order(created_at: :desc) }
```

**Tools:** Manual Review
