Hi another quick question. i have a relationship "areas has_many elements" (elements acts_as_list) i want to retrive those elements ordered by element.position elements = area.elements elements.each do |element| is there a quick and elegent way to it (like area.elements.orderby) or is an SQL query a must. like : Element.find(:all, condition => ......., order => ... -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
sohrauer-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2007-Dec-13 14:52 UTC
Re: sorting the children of an object
http://www.ruby-doc.org/core/classes/Array.html#M002208 Something like: elements.sort{ |x,y| x.position <=> y.position } On Dec 13, 3:20 pm, Gady Sujo <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi > another quick question. > i have a relationship "areas has_many elements" (elements acts_as_list) > i want to retrive those elements ordered by element.position > > elements = area.elements > elements.each do |element| > > is there a quick and elegent way to it (like area.elements.orderby) > or is an SQL query a must. > > like : > Element.find(:all, condition => ......., order => ... > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You can actually do a find like such: elements = area.elements.find(:all, :order => "name") and it will automatically scope only to elements in the area. Still a ''query'', but a very simple one. Michael On Dec 13, 2007, at 9:20 AM, Gady Sujo wrote:> > Hi > another quick question. > i have a relationship "areas has_many elements" (elements > acts_as_list) > i want to retrive those elements ordered by element.position > > elements = area.elements > elements.each do |element| > > is there a quick and elegent way to it (like area.elements.orderby) > or is an SQL query a must. > > like : > Element.find(:all, condition => ......., order => ... > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
thanks. these are good solutions. which one is better optimized ? Michael Bleigh wrote:> You can actually do a find like such: > > elements = area.elements.find(:all, :order => "name") > > and it will automatically scope only to elements in the area. Still a > ''query'', but a very simple one. > > Michael-- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
you can also sort through the association has_many :elements, :order => :position so area.elements will always be sorted. On Dec 13, 10:33 am, Gady Sujo <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> thanks. > these are good solutions. > which one is better optimized ? > > Michael Bleigh wrote: > > You can actually do a find like such: > > > elements = area.elements.find(:all, :order => "name") > > > and it will automatically scope only to elements in the area. Still a > > ''query'', but a very simple one. > > > Michael > > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
They are negligibly different in terms of efficiency. The has_many :elements, :order => :name has a simplicity that I like, I would go with that one. Michael Bleigh michael-+08J6pdAJjhWk0Htik3J/w@public.gmane.org On Dec 13, 2007, at 10:33 AM, Gady Sujo wrote:> > thanks. > these are good solutions. > which one is better optimized ? > Michael Bleigh wrote: >> You can actually do a find like such: >> >> elements = area.elements.find(:all, :order => "name") >> >> and it will automatically scope only to elements in the area. Still a >> ''query'', but a very simple one. >> >> Michael > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---