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

ActiveRecord::QueryMethods#reorder with nil argument still removes the default_scope order #14308

Closed
jhephs opened this issue Mar 7, 2014 · 4 comments

Comments

@jhephs
Copy link

jhephs commented Mar 7, 2014

I'm using Rails 4.1.0.rc1 and I have the following

class User < ActiveRecord::Base
  default_scope { order(:created_at => :desc) }
end

class UsersController < ApplicationController
  def index
    @users = User.reorder(params[:order])
  end
end

> User.all.to_sql
 => "SELECT `users`.* FROM `users`   ORDER BY `users`.`created_at` DESC"
> User.reorder(nil).to_sql
 => "SELECT `users`.* FROM `users`"

I don't know if this is more of a feature request but I'm guessing that this is somehow common. I expect the reorder method not to remove the default order if the passed argument is nil.

@pftg
Copy link
Contributor

pftg commented Mar 7, 2014

reorder(nil) is common way to clean order, so this is expected behaviour.

@jhephs
Copy link
Author

jhephs commented Mar 7, 2014

@pftg Can't we use except(:order) or unscope(:order) to clean order instead?

@pftg
Copy link
Contributor

pftg commented Mar 7, 2014

yep, you can use it
FYI, if you will create PR to change this behaviour:

P.S. please re-ask this question on ruby on rails core mailing list

@robin850
Copy link
Member

robin850 commented Mar 7, 2014

#reorder is meant to remove all existing orders:

# Replaces *any* existing order defined on the relation with the specified order.

You can post your feature request to the rails-core mailing list as pointed by @pftg but I'm not sure that this is worth and actually achievable to be honest. Thank you guys!

@robin850 robin850 closed this as completed Mar 7, 2014
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

3 participants