Group created_at cweek in rails where clause

I am trying to analyze some historical data week after week.

Currently in use

Model.where(:created_at => 3.weeks.ago..2.weeks.ago)
Model.where(:created_at => 2.weeks.ago..1.week.ago)
..etc

      

I want to parse cweek.

Is there a way to do this intelligently? those.

Model.where(:created_at.cweek => Date.Now.cweek)

      

I'm sure there is a very easy way to do this.

+3


source to share


2 answers


To find all records for a specific year and week, you can use:



week_range = Date.commercial(2014, 35).to_time.all_week
Model.where(created_at: week_range)

      

+3


source


Solved it this way:

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 above answer may have a more general application and actually answers my original question better, but with that I don't even need to know the book.

0


source







All Articles