Skip to content

Instantly share code, notes, and snippets.

@mikedijkstra
Last active August 29, 2015 14:16
Show Gist options
  • Save mikedijkstra/04a7c1acb6b1e434e55d to your computer and use it in GitHub Desktop.
Save mikedijkstra/04a7c1acb6b1e434e55d to your computer and use it in GitHub Desktop.
Rails: Group by day showing empty days
notifications = Notifications.where('notifications.created_at BETWEEN ? AND ?', start_date, end_date)
notifications_by_date = notifications.group_by { |notification| notification.created_at.to_date }
@notifications_by_day = (start_date.to_date..end_date.to_date).map do |date|
{
day: date,
notifications: notifications_by_date[date] || [],
}
end
@notifications_by_day.reverse!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment