I have ran into a stumbling block sorting a group_by In my model, I have defined a method called "due_by_in_words", this method returns a hash with a :key and a :value based on a number of elsif statements. {:key => 1, :value => ''Overdue''} {:key => 2, :value => ''Today''} etc... In my controller I have: @tickets_due_by = @tickets.group_by { |t| t.due_by_in_words[:key] } Which is grouping based on the hashes :key, this is working very well. What I want to do now, is to display the :value from the hash, in my view I have: <% @tickets_due_by.sort.each do |due_by_in_words, tickets| %> <h2><%= due_by_in_words %></h2> <% for ticket in tickets %> HTML here... <% end %> <% end %> This is, of course, only displaying the :key (1,2,3...), I would like to be able to access the :value of the hash, but it won''t be accessible to the view, at least I don''t think it will be. It seems to me that the "due_by_in_words" will only contain the :key of the hash, and not the entire hash. I want to be able to display the :value as a header of sorts Any thoughts? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
ESPNDev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-May-02 00:52 UTC
Re: Sorting group_by
Alright. Couple things. Tickets should have an attribute :due_by that stores when it''s due. due_by should return a hash of due ids and descriptions. Keep in mind, when writing hashes, you don''t explicitly define :key and :value: @due_by = {1 => ''Overdue'', 2 => ''Today'',...} In your controller you should have: @tickets_due_by = @tickets.group_by{|t| t.due_by} Then in your view <% @tickets_due_by.sort.each do |due_by_id, tickets| %> <h2><%= @due_by[due_by_id] %></h2> <% for ticket in tickets %> <%= ticket.name %> <% end %> <% end %> Basically it involves writing your hash correctly :) ESPNDev On May 1, 3:23 pm, Shane <shane.pinn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have ran into a stumbling block sorting a group_by > > In my model, I have defined a method called "due_by_in_words", this > method returns a hash with a :key and a :value based on a number of > elsif statements. > > {:key => 1, :value => ''Overdue''} > {:key => 2, :value => ''Today''} > etc... > > In my controller I have: > > @tickets_due_by = @tickets.group_by { |t| t.due_by_in_words[:key] } > > Which is grouping based on the hashes :key, this is working very well. > > What I want to do now, is to display the :value from the hash, in my > view I have: > > <% @tickets_due_by.sort.each do |due_by_in_words, tickets| %> > <h2><%= due_by_in_words %></h2> > <% for ticket in tickets %> > HTML here... > <% end %> > <% end %> > > This is, of course, only displaying the :key (1,2,3...), I would like > to be able to access the :value of the hash, but it won''t be > accessible to the view, at least I don''t think it will be. It seems > to me that the "due_by_in_words" will only contain the :key of the > hash, and not the entire hash. > > I want to be able to display the :value as a header of sorts > > Any thoughts?--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---