Ruby on Rails group_by (how to group events by month)
def index
@events = Event.all
@event_months = @events.group_by { |t| t.due_at.month }
end
Month Name in view is given by
Date::MONTHNAMES[month]
group by month rails ruby
I believe you forgot to sort events in the query itself
class EventsController < ApplicationController
def index
@events = Event.all.order(:date)
# HERE: You don't need to enclose that in array, that's why it fails in view
@events_by_month = @events.group_by { |x| x.date.beginning_of_month }
end
Then in view you can do
<% @events_by_month.each do |begin_month, events| %>
<h2><%= begin_month.strftime("%B %Y") %></h2>
<% events.each do |firm| %>
<p> <%= firm.name %> <%= firm.date.to_date.to_s(:long) %></P>
<% end %><br>
<% end %>
No need to sort in view now, it's already sorted (and database it's much more performant in doing this)
Using group_by beginning_of_month, how do I get an event to show again in the next month?
One approach you could take is to first define the list of all months in the range to be displayed, and then associate all events with that month.
You may wish to tweak the code below for your needs - e.g. if you don't want to show events in the past, or in the next calendar year:
first_start_month = Event.minimum(:start_date).beginning_of_month
last_end_month = Event.maximum(:end_date).beginning_of_month
months = [first_start_date]
while(months.last != last_end_month) do
months << months.last + 1.month
end
@monthly_events = months.map do |m|
[m, @events.select { |e| (e.start_date .. e.end_date).include?(m) }]
end.to_h
With this, your view code can remain basically unchanged:
<% @monthly_events.each do |month, events| %>
<h2><%= month.strftime('%B') %></h2>
<% events.each do |event| %>
<li><%= event.title %></li>
<%= event.start_date %>
<%= event.end_date %>
<% end %>
<% end %>
Group ruby array in rails by year and month
You want to group events by month and year as I understood.
Then use@events_by_month = @events.group_by { |x| [x.date.month, x.date.year] }
you'll receive
{[3, 2014]=>[#<Event id: 7032, date: "2014-03-02 00:00:00">]},{[3,2015]=> [#<Event id: 7033, date: "2015-03-02 00:00:00">]}
It will be more correct then duplicated keys in your example.
SQLite: group records by month they were created and get a count of records per month
My Bloc.io mentor led me to this: http://railscasts.com/episodes/29-group-by-month
So I can do something like this to return a hash with months as the keys:
months = Event.all.group_by { |t| t.created_at.beginning_of_month }
Rails group_by showing multiple times
By using:
<%= render @events %>
Rails is iterating all of your events and rendering the _event.html.erb partial each time.
You could try placing the event_months code in its own partial, in the index.html.erb, or in another location.
group_by multiple times in ruby
I think following will work for you,
events = [
{ name: 'Event 1', date: '2019-02-21 08:00:00', area: 'South', micro_area: 'A' }
]
events.group_by { |x| x[:date] }.transform_values do |v1|
v1.group_by { |y| y[:area] }.transform_values do |v2|
v2.group_by { |z| z[:micro_area] }
end
end
# {
# "2019-02-21 08:00:00"=>{
# "South"=>{
# "A"=>[
# {:name=>"Event 1", :date=>"2019-02-21 08:00:00", :area=>"South", :micro_area=>"A"}
# ]
# }
# }
# }
Related Topics
Ide Sublime2 How to Find Method Definition
How to Output My Ruby Commandline Text in Different Colours
Is It a Bad Idea Do Divide the Models into Directories
Differencebetween Gsub and Sub Methods for Ruby Strings
Render Erb from Database into View Problem Please Help!
No Such File to Load -- Soap4R -- Why
Rails 3 - Has_And_Belongs_To_Many
Ruby: How to Process a CSV File with "Bad Commas"
Using Devise for More Than One Models in Rails App
Devise 'Find_First_By_Auth_Conditions' Method Explanation
How to Use Link_To with Ajax in Rails 3.2.1
Marshal (Ruby) Pipes: Sending Serialized Object to Child Processes
Nomethoderror on Section 5.7 of Rails Guide