Faveod / arel-extensions

Extending Arel
MIT License
142 stars 24 forks source link

order ignores multiple nulls_last or nulls_first #41

Closed joshleblanc closed 3 years ago

joshleblanc commented 3 years ago

Expected behavior

It should also order by `examples.updated_id DESC NULLS LAST``

Actual behavior

The second order statement is ignored completely.

System configuration

Rails version: 6.1.3

Ruby version: ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x64-mingw32]

The following is a reproduction, you can test with/without arel_extensions by removing the arel_extensions from the gemfile.

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem 'arel_extensions'
  gem "activerecord", "~> 6.1"
  gem "pg"
end

require "active_record"
require "minitest/autorun"
require "logger"

ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "activerecord_unittest", username: "postgres", password: "postgres")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :examples, force: true do |t|
    t.belongs_to :parent
    t.timestamps
  end
end

class Example < ActiveRecord::Base
  belongs_to :parent
end

class BugTest < Minitest::Test
  def test_order_arel
    sql = %{SELECT "examples".* FROM "examples" ORDER BY "examples"."id" DESC NULLS LAST, "examples"."updated_at" DESC NULLS LAST}

    assert_equal sql, Example.order(Example.arel_table[:id].desc.nulls_last).order(Example.arel_table[:updated_at].desc.nulls_last).to_sql
  end
end

This was originally posted here: https://github.com/rails/rails/issues/41531

jdelporte commented 3 years ago

Hi Josh, Thank you for your contribution. The version 2.0.21 fixes this issue.