Reputation: 2195
Im trying to analyse some data on a historical week by week basis.
Currently using
Model.where(:created_at => 3.weeks.ago..2.weeks.ago)
Model.where(:created_at => 2.weeks.ago..1.week.ago)
..etc
I want to analyse by cweek.
Is there a way to do that sensibly? i.e.
Model.where(:created_at.cweek => Date.Now.cweek)
I'm sure there's a very simple way to do this.
Upvotes: 3
Views: 158
Reputation: 2195
Solved it like this:
application_helper.rb
def weeks_ago_range(number)
number.weeks.ago.all_week
end
Then
Model.where(:created_at => weeks_ago_range(1)).count
I think the answer above might have more general use and actually answers my original question better, but with this I don't even need to know the cweek.
Upvotes: 0
Reputation: 114178
To find all records within a given year and week you could use:
week_range = Date.commercial(2014, 35).to_time.all_week
Model.where(created_at: week_range)
Upvotes: 3