Sorting group_by

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'}

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?

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 %>
    <%= %>
  <% end %>
<% end %>

Basically it involves writing your hash correctly :slight_smile: