Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

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

#5528

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.

Reported by Nick Ragaz · September 2nd, 2010 @ 05:43 AM

State: open
Milestone: 3.1
Assigned to: Santiago Pastorino Santiago Pastorino
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh
    • 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.

    September 2nd, 2010 @ 06:05 AM

  2. Nick Ragaz
    Nick Ragaz

    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...

    September 2nd, 2010 @ 06:08 AM

  3. Santiago Pastorino
    Santiago Pastorino
    • Milestone cleared.
    • State changed from hold to open

    September 5th, 2010 @ 02:53 AM

  4. José Valim
    José Valim

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

    September 5th, 2010 @ 09:03 AM

  5. Santiago Pastorino
  6. Nick Ragaz
    Nick Ragaz

    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"
    

    September 17th, 2010 @ 04:36 AM

  7. Jeremy Kemper
  8. Santiago Pastorino
    Santiago Pastorino
    • 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 .

    March 28th, 2011 @ 03:27 PM