All threads / How do you group by week for a month of records, but only find dates for the month.

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do you group by week for a month of records, but only find dates for the month.

Darion Wood asked in Rails

arget_date = Date.today.beginning_of_month
target_range = target_date..target_date.end_of_month

Post.where(created_at: target_range).group_by_week(:created_at, range: target_range).sum(:view_count)
I'm using group_by_week() that is included with groupdate gem. The issue is, its returning records from the end of the last month for certain months. Given that certain months begin on Wednesday. I would like to get all record for example: The month of July 1st-31st and then get the sums for each week within the month. Week 1, should start on July 1st.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 45,185+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2021 GoRails, LLC. All rights reserved.