Current situation with chained order scopes

I have found various blogs and comments on the issue of merging order
scopes but have not been able to determine whether the current
situation is regarded as satisfactory.

Using Rails 3.0.4, the particular case I am considering is
Item.order( ‘x DESC’).order(‘x ASC’)
This appears to merge the sorts in the wrong order so that the result
is x DESC. The sql generated is
SELECT ‘items’.* FROM ‘items’ ORDER BY id DESC, id ASC
Which results in descending order (SQL is rather non-intuitive here).

One result is that it is not possible (as far as I can see) to
override a default or named scope that applies a sort order.

Perhaps I am missing something obvious.

Colin

Actually it’s doing multiple field ordering… It’s best to think of the
orders as compounding not replacing each other. Easiest to see if you
have two different cols rather than the same one… For example
created_at desc, name asc will sort on creation date and then name… So
for all records where the dates are identical, it will then sort on name
alpha ascending… Understand?

Blog: http://random8.zenunit.com/
Twitter: http://twitter.com/random8r
Learn: http://sensei.zenunit.com/
New video up now at http://sensei.zenunit.com/ real fastcgi rails deploy
process! Check it out now!

If you want to change the order instead of adding another column to
ordering list then there is reorder() method which is however
deprecated in Rails 3.1 in favor of except(:order).order(new order
here)

Robert Pankowecki

On 16 February 2011 19:45, Julian L. [email protected]
wrote:

Actually it’s doing multiple field ordering… It’s best to think of the orders
as compounding not replacing each other. Easiest to see if you have two different
cols rather than the same one… For example created_at desc, name asc will sort
on creation date and then name… So for all records where the dates are
identical, it will then sort on name alpha ascending… Understand?

Yes, I understand how it works in that situation. It is the problem
of overriding a previous scope that concerns me. I see how, in order
for it to work as you describe, it is difficult for it to do anything
else in the situation where mutliple conflicting orders for the same
field applies. But see response to Robert’s post.

Colin

On 17 February 2011 00:53, Robert Pankowecki (rupert)
[email protected] wrote:

If you want to change the order instead of adding another column to
ordering list then there is reorder() method which is however
deprecated in Rails 3.1 in favor of except(:order).order(new order
here)

That is what I wanted, I had missed it previously. So, to clarify for
anyone picking this up in the future, if we have
Items = Item.order( :id )
then we can say
items.except(:order).order( ‘id DESC’ )
to override the previous sort.

I have tried it in the console and it works as expected.

Many thanks

Colin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs