

Rails 7 supports NullsFirst for all databases
source link: https://blog.saeloun.com/2021/10/12/support-nulls_first-for-all-databases
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In the beginning of this year, Rails 6.1 added support for nulls_first and nulls_last Arel methods for PostgreSQL. This was a great addition since, unlike most databases, PostgreSQL would push data with null values to the top when ordering columns. However, in most situations, this was not preferred.
Before
Rails 6.1 introduced the following Arel methods only to PostgreSQL databases to control null data ordering.
irb> User.arel_table[:name].desc.nulls_last
(0.9 ms) SELECT "users"."name" FROM "users"
ORDER BY name DESC NULLS LAST
=> [["Adam Driver"], ["Bob Dylan"], [""]]
This was fantastic for PostgreSQL users as the default is set to “NULLS FIRST”. Unfortunately, all other databases were missing out on this.
After
Fortunately, ANSI SQL has an option to allow the database to specify where NULLS come out in the sort order,
ORDER BY column ASC NULLS FIRST
MS SQL, SQLite, Oracle, and PostgreSQL all follow this syntax. Unfortunately, MySQL does not.
Rails 7 now introduces support for nulls_first() and nulls_last()
for all major databases, except MySQL.
When trying to use nulls_last()
on MySQL databases, it will throw a runtime error.
Recommend
-
11
Rails multiple databases support in Rails Event Store Rails 6 released in August 2019 has brought us several new features. One of the notable ch...
-
4
Storybook Frameworks You Can Use to Build Component Libraries
-
8
-
59
Amazon RDS now supports T4g instances for MySQL, MariaDB, and PostgreSQL databases. Learn About AWS Pricin...
-
5
rails_7 Published on 6 October 2021 6 October 2021 • 2 min read...
-
12
rails_7 Published on 28 January 2022 ...
-
5
Multiple databases in a single query in your Rails apps - Postgres Foreign Data Wrappers to the rescue Jun 27, 2021 Imagine that you are building a separate applic...
-
9
Rails now supports infinite range options in LengthValidators Oct 19, 2022 , by Swaathi Kakarla 2 minute read ...
-
8
Rails now supports routes prefixed with word cable Dec 8, 2022 , by Prasanth Chaduvula 1 minute read ...
-
5
Rails provides the has_secure_password method, which makes it gloriously easy to implement au...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK