---
title:
  "Rails 6.1 adds nulls_first and nulls_last methods to Arel for PostgreSQL"
description:
  "Rails 6.1 adds nulls_first and nulls_last methods to Arel for PostgreSQL"
canonical_url: "https://www.bigbinary.com/blog/rails-6-1-adds-nulls-first-and-nulls-last-to-arel"
markdown_url: "https://www.bigbinary.com/blog/rails-6-1-adds-nulls-first-and-nulls-last-to-arel.md"
---

# Rails 6.1 adds nulls_first and nulls_last methods to Arel for PostgreSQL

Rails 6.1 adds nulls_first and nulls_last methods to Arel for PostgreSQL

- Author: Berin Larson
- Published: March 9, 2021
- Categories: Rails, Rails 6.1

In PostgreSQL, when sorting output rows in descending order, columns with null
values will appear first.

Let's take this example of ordering users by the number of times they have
logged in.

```sql

postgres=> SELECT * from users ORDER BY login_count DESC;

       name        | login_count
-------------------+-------------
 Johnny Silverhand |        NULL
 Jackie Welles     |         202
 V                 |           1

(3 rows)

```

This is not useful since most of the time we would want the null values to
appear last.

PostgreSQL provides `NULLS FIRST` and `NULLS LAST` options for the `ORDER BY`
clause for this use case.

```ruby

irb> pp User.order("login_count DESC NULLS LAST").
        pluck(:name, :login_count)

(0.9 ms)  SELECT "users"."name", "users"."login_count" FROM "users"
          ORDER BY login_count DESC NULLS LAST

=> [["Jackie Welles", 202],
   ["V", 1],
   ["Johnny Silverhand", nil]]

```

In Rails 6.1, we can use the new `nulls_first` or `nulls_last` methods to
construct the same query using Arel.

```ruby

irb> pp User.order(User.arel_table[:login_count].desc.nulls_last).
        pluck(:name, :login_count)

(0.9 ms)  SELECT "users"."name", "users"."login_count" FROM "users"
          ORDER BY login_count DESC NULLS LAST

=> [["Jackie Welles", 202],
   ["V", 1],
   ["Johnny Silverhand", nil]]

```

The resulting code is slightly more verbose for this simple example. But Arel
really shines when programmatically constructing complex SQL queries.

Check out this [pull request](https://github.com/rails/rails/pull/38131) for
more details.

## Links

- [Human page](https://www.bigbinary.com/blog/rails-6-1-adds-nulls-first-and-nulls-last-to-arel)
