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

Rails 7: implicit_order_column does not order records #44621

Closed
john-999 opened this issue Mar 5, 2022 · 3 comments
Closed

Rails 7: implicit_order_column does not order records #44621

john-999 opened this issue Mar 5, 2022 · 3 comments

Comments

@john-999
Copy link

john-999 commented Mar 5, 2022

How to to reproduce:

In the file app/models/application_record.rb, add a implicit_order_column as follows:

class ApplicationRecord < ActiveRecord::Base
  primary_abstract_class
  self.implicit_order_column = 'created_at'
end

A query like...

User.all

...will fail to return the records in the created_at order.

System configuration

Rails version: Rails 7.0.2.2

Ruby version: ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [x86_64-linux]

@fatkodima
Copy link
Member

#34480 - It was meant that this is applied to ordered finder methods like first/last.

So, this is working as expected. I think I would be surprised if .all will be implicitly ordered.

@john-999
Copy link
Author

john-999 commented Mar 5, 2022

It is my understanding that the most prominent use case of implicit_order_column is to accommodate the use of UUID as the primary key, in which case it seems natural to me to expect the .all query to support the same logic as .first and .last.

But that's just... like... my opinion, man. ;-)

@eugeneius
Copy link
Member

As @fatkodima mentioned, this is working as intended (and documented):

https://api.rubyonrails.org/classes/ActiveRecord/ModelSchema.html#method-c-implicit_order_column

The name of the column records are ordered by if no explicit order clause is used during an ordered finder call.

all is not an ordered finder call. Note that all executes SELECT * FROM table, with no order clause; first and last order by primary key by default, which is problematic when using UUIDs.

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

4 participants
@eugeneius @john-999 @fatkodima and others