Skip to main content

Rails: Grouping the records by group_by method

Many times we come across such situations where we need to group records, like when we want to display grouped messages, email, alerts .etc based on date or time. Here is the simple code which provides a significant use of group_by method.

Controller Part

class MessagesController < ApplicationController
 def index
   @message = Message.all

   #Retrives all messages and divides into two groups todays messages and other messages
   @grouped_messages = @message.group_by{ |t| t.created_at.to_date == DateTime.now.to_date }

   if @grouped_messages[false].present?
     #Create month wise groups of messages      
     @month_wise_sorted_alerts  = @grouped_messages[false].group_by{ |t| t.created_at.month}
   end    
 end
end


@message.group_by{ |t| t.created_at.to_date == DateTime.now.to_date }


The above line return us the messages in Ordered Hash with two keys true and false on which the messages which are of today's date will be values in true key and others will be values in false.


@grouped_messages[false].group_by{ |t| t.created_at.month }


Above line will provide us the monthwise sorting of the messsages. It will create the Ordered Hash with keys as the month numbers and values as the messages according to their month numbers.


View Part


<h1>Messages</h1>

<!-- Todays messages -->

<% if @grouped_messages.present? && @grouped_messages[true].present? %>
 <h3> Today </h3>
 <% @grouped_messages[true].each do |msg| %>
   <%= msg.content %>
   <%= msg.created_at.strftime('%I:%M %p') %>
   <br />
 <% end %>
 <br />
<% end %>


<!-- Month wise sorted messages -->

<% if @month_wise_sorted_alerts.present? %>
 <% @month_wise_sorted_alerts.each do |hash_elements|%>
 <h3> <%= Date::MONTHNAMES[hash_elements.first] %> </h3>
 <% hash_elements.last.each do |msg| %>
   <%= msg.content %>
   <%= msg.created_at.strftime('%b %d') %>
   <br />
 <% end %>
 <br />
 <% end %>
<% end %>


We can use rails helper to get the month name Date::MONTHNAMES[--month_number--] to get the name of the month in the view.



Grouped Messages

You can also use the gem https://github.com/ankane/groupdate to get more functionality.

References:

  • http://apidock.com/rails/Enumerable/group_by

Comments

  1. Good stuff with a scope for improvement. This is very specific implementation with an opportunity to be generalized for different purposes. For example, the same code/logic can be used for month_wise_sorted_alerts and grouped messages.

    ReplyDelete
  2. Hi, just wanted to tell you, I enjoyed this blog post.

    ReplyDelete
  3. What a commendable work you have done, with simplest of language. Great piece of writing, Awesome stuff.

    THE #1 MEDICAL WEBSITE DESIGN for all kind of medical offices and specialists.

    ReplyDelete

Post a Comment

Popular posts from this blog

List of Common mistakes in Ruby

Always use spaces around operators, after commas, colons and semicolons, around { and before }. White space might be (mostly) irrelevant to the Ruby interpreter, but its proper use is the key to writing easily readable code. Also it makes the design more readable and code much cleaner.
product = 1 * 2 array = [1, 2, 3, 4, 5] array.map { |a| a + 2 }

There should be no spaces after (, [ or before ], ) these brackets.
['ankur', 'vyas'] sum(a, b)
Also don't use spaces in while providing the range.
Use5..9 over 5 .. 9Use'a'..'z' over 'a' .. 'z'
When using switch case statements use the following indentation.
casewhen input = '+'puts'The operation is addition'when'-'puts'The operation is subtraction'when'*'puts'The operation is multiplication'elseputs'The operation is division'end
Use following indentation if the parameters exceeds to more than one line.
defsend_mail(source) Mailer.deliver(…

5 Useful Android Apps

There are many apps available for Android but these are some useful apps which I use regularly.
1. Money View: Financial Planning
Description: With the Free Money View app, you get a real-time visibility into your entire Personal Finances. It works by itself without any manual data entry - their daily expense manager app organizes our financial summary by analyzing SMS sent by our bank and billers to our phone. The app auto-tracks your expense, bills and account balances across all our financial accounts to help us stay on top of our money. The app is highly secure as it never reads any sensitive data - no full bank account numbers, no OTP, and no Netbanking login/password.
As it anaylzes the money transactions via the SMS in our phone it is highly useful app for tracking our expenses, so we can know our money is going in which direction so we can control it.




More information:  https://play.google.com/store/apps/details?id=com.whizdm.moneyview&hl=en

2. Any.do To-do List | Task List
Descr…

OOP Polymorphism

Definition: "Poly" stands for "many" and "morph" stands for "forms". Generally, polymorphism means one name different uses. Technically, it means being able to send the same message to different objects and get different result.
Polymorphism through Inheritance
We can achieve polymorphism through inheritance. For example
classGenericParser defparseraiseNotImplementedError, 'You must implement the parse method'endendclassJsonParser < GenericParserdefparseputs'An instance of the JsonParser class received the parse message'endendclassXmlParser < GenericParserdefparseputs'An instance of the XmlParser class received the parse message'endendHere the GenericParser is the base class. Class JsonParser and XmlParser are inherited from the GenericParser.
Now suppose we run the below code.
puts'Using the XmlParser' parser = XmlParser.new parser.parse puts'Using the JsonParser' parser = JsonParser.new parser.parseThe o…