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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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! On 17/02/2011, at 4:38 AM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 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 > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Pankowecki (rupert)
2011-Feb-17 00:53 UTC
Re: Current situation with chained order scopes
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 16 February 2011 19:45, Julian Leviston <julian-AfxEtdRqmE/tt0EhB6fy4g@public.gmane.org> 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> > 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! > > > On 17/02/2011, at 4:38 AM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > >> 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 >> >> -- >> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 17 February 2011 00:53, Robert Pankowecki (rupert) <robert.pankowecki-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.