Is there any way in a scope to order a list by a text field ("title", for instance) but put the item that is titled "Other" at the end of the list? Thanks -- 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.
Marnen Laibow-Koser
2010-Oct-27 16:09 UTC
Re: Order by "title" but then put "Other" at the end?
Andy wrote in post #957534:> Is there any way in a scope to order a list by a text field ("title", > for instance) but put the item that is titled "Other" at the end of > the list?The easiest way would involve using a separate DB field, so that you can do ORDER BY title, put_at_end.> > ThanksBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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.
Philip Hallstrom
2010-Oct-27 16:16 UTC
Re: Order by "title" but then put "Other" at the end?
> Is there any way in a scope to order a list by a text field ("title", > for instance) but put the item that is titled "Other" at the end of > the list?Assuming that the item is always titled ''Other'' and it will never be something else and you''re okay with cheating a little bit this would work for Rails 3... scope :almost_by_title, order("title = ''Other'', title") This gets messy pretty fast. For instance when you have to add ''Unknown'' and ''Misc'' to the end as well. But if it''s all you need you can get away with it :) As Marnen said, setup another "sortable_title" field and order on that. The upside to doing this is you can also strip "A", "The", and "An" from the beginning of titles so they sort more naturally. -philip -- 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.
Marnen Laibow-Koser
2010-Oct-27 16:39 UTC
Re: Order by "title" but then put "Other" at the end?
Philip Hallstrom wrote in post #957539:>> Is there any way in a scope to order a list by a text field ("title", >> for instance) but put the item that is titled "Other" at the end of >> the list? > > Assuming that the item is always titled ''Other'' and it will never be > something else and you''re okay with cheating a little bit this would > work for Rails 3... > > scope :almost_by_title, order("title = ''Other'', title")Ooh, I like that.> > This gets messy pretty fast. For instance when you have to add > ''Unknown'' and ''Misc'' to the end as well. But if it''s all you need you > can get away with it :) > > As Marnen said, setup another "sortable_title" field and order on that.That''s not what I said. I was thinking of a Boolean put_at_end field.> The upside to doing this is you can also strip "A", "The", and "An" from > the beginning of titles so they sort more naturally.Excellent point.> > -philipBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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 Oct 27, 9:09 am, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> The easiest way would involve using a separate DB field, so that you can > do ORDER BY title, put_at_end.Wouldn''t that have to be ORDER BY put_at_end, title? (assuming put_at_end has a value of 0 or 1) -- 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.
Marnen Laibow-Koser
2010-Oct-27 17:51 UTC
Re: Order by "title" but then put "Other" at the end?
E. Litwin wrote in post #957563:> On Oct 27, 9:09am, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> The easiest way would involve using a separate DB field, so that you can >> do ORDER BY title, put_at_end. > > Wouldn''t that have to be ORDER BY put_at_end, title? (assuming > put_at_end has a value of 0 or 1)Yes, it would. Must think a little more before I type. :D Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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.
I solved this in an application by creating a separate "sort_name" field that was strictly used for sorting. Never seen or used by the user at all. We had a "before_save :build_sort_name" method that handled all sorts of cases. Wouldn''t be too hard to add something similar for your scenario. def build_sort_name sort_name = name.gsub(/^(The )(.*)/, ''\2, \1'') # change "The Title" to "Title, The " sort_name = "ZZZZZZZZZZZZZZ Other" if name = ''Other'' # force ''Other'' to sort after everything else end -- 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.