This project is archived and is in readonly mode.

#5528 open
Nick Ragaz

scopes using `reorder` don't override `default_scope` order

Reported by Nick Ragaz | September 2nd, 2010 @ 05:43 AM | in 3.1

Until this commit I believe on 3.0.1.pre using reorder in a scope would override an order clause in the default_scope. Now the reorder is appended instead, meaning that it's impossible to do something like

default_scope order('name')
scope :reversed, reorder('name DESC')

I don't believe this is correct behaviour and have attached a patch against the 3-0-stable branch that includes a failing test - this case does not appear to have any tests associated with it to verify the expected behaviour.

Comments and changes to this ticket

  • Neeraj Singh

    Neeraj Singh September 2nd, 2010 @ 06:05 AM

    • Milestone set to 3.x
    • State changed from “new” to “hold”
    • Assigned user set to “Neeraj Singh”
    • Importance changed from “” to “Low”

    Thanks for reporting the bug. There is a discussion going on about how to handle default_scope in ticket #5519. Until that ticket is resolved I am putting this on on hold.

  • Nick Ragaz

    Nick Ragaz September 2nd, 2010 @ 06:08 AM

    Thanks for the quick review. I guess my view would be that the "shipped" API in v3.0 should be honoured, and I'm pretty sure that this behaviour was introduced accidentally...

  • Santiago Pastorino

    Santiago Pastorino September 5th, 2010 @ 02:53 AM

    • Milestone cleared.
    • State changed from “hold” to “open”
  • José Valim

    José Valim September 5th, 2010 @ 09:03 AM

    Actually, reorder should always override the default scope. The current state is definitely a bug.

  • Santiago Pastorino
  • Nick Ragaz

    Nick Ragaz September 17th, 2010 @ 04:36 AM

    Sadly, there is still an issue - at least, I think there is - if you have multiple scopes chained, like so:

    class Foo < ActiveRecord::Base
        default_scope order('id')
        scope :name_is_nil, where(:name => nil)
        scope :name_order, reorder('name')
        scope :age_order, reorder('age')
    end
    
    Foo.name_order.age_order
    

    You will get SQL like:

    SELECT * FROM foos ORDER BY name, age
    

    Where I would expect:

    SELECT * FROM foos ORDER BY age
    

    i.e. that the last reorder scope in the chain would take precedence.

    Also, the current behaviour will produce:

    Foo.name_order.name_is_nil.age_order.to_sql # => "SELECT * FROM foos ORDER BY name, id, age"
    
  • Jeremy Kemper

    Jeremy Kemper October 15th, 2010 @ 11:02 PM

    • Milestone set to 3.0.2
  • Santiago Pastorino

    Santiago Pastorino March 28th, 2011 @ 03:27 PM

    • State changed from “committed” to “open”
    • Milestone changed from 3.0.2 to 3.1
    • Assigned user changed from “Neeraj Singh” to “Santiago Pastorino”

    This is actually a bug in 3.1 and the solution of 3.0.x is the ugliest I've ever seen. I can say that without worrying about hurting anybody's feelings since I've made it .

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages