Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group by week: Possible for week_start to be the first day in a given range? #265

Closed
wtholt opened this issue Nov 15, 2022 · 2 comments
Closed

Comments

@wtholt
Copy link

wtholt commented Nov 15, 2022

Hi,

Thanks for all your work on this gem. I've enjoyed using it so far.

I was wondering if there is a way I could have the week_start when grouping by week begin with the first day of the given range?

For example:

relation = Object.where(created_at: 2022-10-15..2022-11-15)

relation.group_by_week(:created_at).count

Right now it defaults to Sunday of a week, so the results would be something like:

2022-10-9 => 5,
2022-10-16 => 5,
2022-10-23 => 5,
2022-10-30 => 5
etc.

Would it be possible for the gem to be smart enough and begin on the date I requested?

So ideally:

2022-10-15=>5,
2022-10-22=>5,
2022-10-29=>5,
2022-11-05=>5
etc

Let me know if this is possible right now with the library, I didn't notice this in the documentation or any other issues.

@ankane
Copy link
Owner

ankane commented Nov 15, 2022

Hey @wtholt, you can do something like:

range = Date.parse("2022-10-15")..Date.parse("2022-11-15")
week_start = range.begin.strftime("%A").downcase.to_sym
relation.group_by_week(:created_at, week_start: week_start, range: range).count

@wtholt
Copy link
Author

wtholt commented Nov 15, 2022

That works perfectly! Thank you!

@wtholt wtholt closed this as completed Nov 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants