Search code examples
ruby-on-railsruby-on-rails-4database-migration

How to create an index that has NULLS LAST in Rails?


I read https://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/add_index and http://guides.rubyonrails.org/active_record_migrations.html.

I don't see how to create an index like:

CREATE INDEX ON users (id DESC NULLS LAST);

Did not see docs on how to create and index with NULLS LAST.

Rails 4.2.10

PostgreSQL


Solution

  • Active Record now supports NULLS LAST in an index with the order argument:

    add_index(
        :my_table,
        [:my_column],
        order: { my_column: "DESC NULLS LAST" }
    )