Find with date range


#1

I have to do some searches with date restrictions such as “this
week,” “last week”, “last month”, etc.
Is there some clever Rails way of doing this, or do I just have to
grind it out?

Thanks,
-George


#2

Rails does provide some core extensions for some of what you need.

5.weeks.ago
Time.now - 20.minutes
Person.created_at - 2.weeks

Not sure what else might be available, but that should help you some.


#3

I have to do some searches with date restrictions such as “this
week,” “last week”, “last month”, etc.
Is there some clever Rails way of doing this, or do I just have to
grind it out?

Chronic can probably do it… never used it, but it comes up a lot in
responses to questions like this:

http://chronic.rubyforge.org/


#4

On Jun 5, 2007, at 3:40 PM, Robert W. wrote:

Rails does provide some core extensions for some of what you need.

5.weeks.ago
Time.now - 20.minutes
Person.created_at - 2.weeks

Yes, i looked at that, but things like “this week” depend on what day
it is, so I can’t do a simple subtraction. Phillip Hallstrom pointed
out Chronic, which looks like it will solve my problem.

Thanks.


#5

Yeah Chronic will solve it,
Chronic.parse(‘this week’) will return the Friday of that week, so you
can then use that in your search,
Chronic.parse(‘this month’) picks the middle of the month etc.

Used it in a project recently, it’s quite cool.

Cam


#6

On Jun 5, 2007, at 3:53 PM, Philip H. wrote:

Chronic can probably do it… never used it, but it comes up a lot in
responses to questions like this:

Thanks Phillip. From the usage examples it looks like it will do the
trick. I did a lot of searching before I sent that email, but somehow
never came across Chronic. I guess I need to work on my Google skills
too. :slight_smile:

-George