Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reordering without explicit direction doesn't work with last in Rails 4 #84

Open
owst opened this issue Jan 2, 2018 · 1 comment
Open

Comments

@owst
Copy link
Contributor

owst commented Jan 2, 2018

This might be an arel bug rather than a baby_squeel bug, apologies if so!

Issue

When using Active Record 4,

Foo.reorder(:x).last

appears to not be equivalent to

Foo.reordering { x }.last

but it is with Active Record 5.

Specifically, it appears that we don't get a DESC in the resulting query in AR 4:

Foo Load (0.1ms)  SELECT  "foos".* FROM "foos"  ORDER BY "foos"."x" LIMIT 1

whereas we do in AR 5:

 Foo Load (0.1ms)  SELECT  "foos".* FROM "foos" ORDER BY "foos"."x" DESC LIMIT ?  [["LIMIT", 1]]

N.B. adding an explicit direction does work in both AR versions:

Foo.reorder('x ASC').last == Foo.reordering { x.asc }.last

Reproduction

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'activerecord', '= 4.2.10' # breaks
  # gem 'activerecord', '= 5.1.4' # works
  gem 'sqlite3'
  gem 'baby_squeel', github: 'rzane/baby_squeel'
end

require 'active_record'
require 'minitest/autorun'
require 'logger'

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')

ActiveRecord::Schema.define do
  create_table :foos do |t|
    t.integer :x
  end
end

class Foo < ActiveRecord::Base
end

ActiveRecord::Base.logger = Logger.new(STDOUT)

class BabySqueelTest < Minitest::Spec
  it 'works' do
    one = Foo.create(x: 1)
    two = Foo.create(x: 2)

    puts "reorder"
    assert_equal one, Foo.reorder('x ASC').first
    assert_equal one, Foo.reorder('x DESC').last
    assert_equal one, Foo.reorder('x').first
    assert_equal two, Foo.reorder('x ASC').last
    assert_equal two, Foo.reorder('x DESC').first
    assert_equal two, Foo.reorder('x').last

    puts "reordering"
    assert_equal one, Foo.reordering { x.asc }.first
    assert_equal one, Foo.reordering { x.desc }.last
    assert_equal one, Foo.reordering { x }.first
    assert_equal two, Foo.reordering { x.asc }.last
    assert_equal two, Foo.reordering { x.desc }.first
    assert_equal two, Foo.reordering { x }.last # Doesn't work with AR 4
  end
end
@bhaak
Copy link

bhaak commented Jun 27, 2018

Just to clarify, this not only affects reordering but also ordering.

bhaak added a commit to bhaak/baby_squeel that referenced this issue Jun 27, 2018
bhaak added a commit to Garaio-REM/baby_squeel that referenced this issue Mar 9, 2020
bhaak pushed a commit to Garaio-REM/baby_squeel that referenced this issue Mar 9, 2020
There is a bug with order and last:
rzane#84
btihen pushed a commit to btihen/baby_squeel that referenced this issue Aug 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants