I should be able to figure this out but perhaps I am brain dead... I simply want to display the next id in sequence for a few ''categories''. My view template is very basic... <% for next_seq in @next_seq %> <%= next_seq[0] %> - <%= next_seq[1] %><br /> <% end %> and I want them sorted in next_seq[0] order and since @next_seq is a hash, it displays in a rather random order. Not that this should matter, but the controller code for this looks like this... @lib_cat = Valuelist.find(:all, :conditions => ["list_name = ''Library Categories''"], :order => ''list_value'') @next_seq = Hash.new for lib_cat in @lib_cat @next_seq[lib_cat.list_value] = Library.find(:first, :conditions => [''ltype = ?'', lib_cat.list_value], :order => ''id DESC'').id + 1 end I''m not doing a ''select'' so I can''t do the normal collect_for_selection thingy Craig --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hashes don''t have an order. The keys come out through iterators in a psuedo random order no matter what. If you want to keep them in order, you have to use an array. You can event make it an array of hashes if you like. -- 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 -~----------~----~----~----~------~----~------~--~---
On 11/18/06, Craig White <craigwhite-BQ75lA0ptkhBDgjK7y7TUQ@public.gmane.org> wrote:> > > I should be able to figure this out but perhaps I am brain dead... > > I simply want to display the next id in sequence for a few ''categories''. > > My view template is very basic... > > <% for next_seq in @next_seq %> > <%= next_seq[0] %> - <%= next_seq[1] %><br /> > <% end %> > > and I want them sorted in next_seq[0] order and since @next_seq is a > hash, it displays in a rather random order. > > Not that this should matter, but the controller code for this looks like > this... > > @lib_cat = Valuelist.find(:all, > :conditions => ["list_name = ''Library Categories''"], > :order => ''list_value'') > @next_seq = Hash.new > for lib_cat in @lib_cat > @next_seq[lib_cat.list_value] = Library.find(:first, > :conditions => [''ltype = ?'', lib_cat.list_value], > :order => ''id DESC'').id + 1 > end > > I''m not doing a ''select'' so I can''t do the normal collect_for_selection > thingyRather than trying to force a hash to return a particular order, why not just do what options_for_select does - use an array of two-element arrays. Your controller would look like this: @lib_cat = Valuelist.find(:all, :conditions => ["list_name = ''Library Categories''"], :order => ''list_value'') @next_seq = Hash.new for lib_cat in @lib_cat @next_seq << [lib_cat.list_value, Library.find(:first, :conditions => [''ltype = ?'', lib_cat.list_value], :order => ''id DESC'').id + 1] end (hardcore functional programmers can replace the for loop with an inject for their own amusement...) The view code would remain the same. -- Matt Jones mdj.acme-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org President/Technical Director, Acme Art Company (acmeartco.org) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Sun, 2006-11-19 at 14:53 -0500, Matt Jones wrote:> > > On 11/18/06, Craig White <craigwhite-BQ75lA0ptkhBDgjK7y7TUQ@public.gmane.org> wrote: > > I should be able to figure this out but perhaps I am brain > dead... > > I simply want to display the next id in sequence for a few > ''categories''. > > My view template is very basic... > > <% for next_seq in @next_seq %> > <%= next_seq[0] %> - <%= next_seq[1] %><br /> > <% end %> > > and I want them sorted in next_seq[0] order and since > @next_seq is a > hash, it displays in a rather random order. > > Not that this should matter, but the controller code for this > looks like > this... > > @lib_cat = Valuelist.find(:all, > :conditions => ["list_name = ''Library Categories''"], > :order => ''list_value'') > @next_seq = Hash.new > for lib_cat in @lib_cat > @next_seq[lib_cat.list_value] = Library.find(:first, > :conditions => [''ltype = ?'', lib_cat.list_value], > :order => ''id DESC'').id + 1 > end > > I''m not doing a ''select'' so I can''t do the normal > collect_for_selection > thingy > > > Rather than trying to force a hash to return a particular order, why > not just do what > options_for_select does - use an array of two-element arrays. > Your controller would look like this: > > @lib_cat = Valuelist.find(:all, > :conditions => ["list_name = ''Library Categories''"], > :order => ''list_value'') > @next_seq = Hash.new > for lib_cat in @lib_cat > @next_seq << [lib_cat.list_value, Library.find(:first, > :conditions => [''ltype = ?'', lib_cat.list_value], > :order => ''id DESC'').id + 1] > end > > > > (hardcore functional programmers can replace the for loop with an > inject for their > own amusement...) > > The view code would remain the same.---- yep - just had to replace @next_seq = Hash.new with @next_seq = Array.new Thanks Craig --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---