RailsCasts Pro episodes are now free!

Learn more or hide this

ryankc33's Profile

GitHub User: ryankc33

Comments by

Avatar

works fantastic with Kaminari AND Rails 4 with turbolinks. even after so many years. Thanks Ryan!

Tip: If you're using Kaminari, just add class="next_page" to the next page link in the Kaminari view

<li class="next_page">
  <%= link_to_unless current_page.last?, raw(t 'views.pagination.next'), url, :rel => 'next', :remote => remote %>
</li>
Avatar

The following code works for grouping counts by month:

ruby
  def self.chart_data(start = 1.year.ago)
    total_count = total_count_by_month(start)
    start = start.to_date.beginning_of_month
    today = Date.today.beginning_of_month
    range = (start..today).select {|d| d.day == 1}
      range.map do |month|
        {
          created_at: month,
          total_enquiries: total_count[month] || 0
      }
    end
  end

  def self.total_count_by_month(start)
    enquiries = unscoped.where(created_at: start.beginning_of_month..Time.now)
    enquiries = enquiries.group("date_trunc('month', created_at)")
    enquiries = enquiries.select("date_trunc('month', created_at) as created_at, count(*) as count")
    enquiries.each_with_object({}) do |enquiry, counts|
      counts[enquiry.created_at.to_date] = enquiry.count
    end
  end
end

date_trunc is a postgresql function.

I've yet to find a way to format the date output to "%b %Y" instead of the default format.

If anyone has a better solution please share!