Forum: Ruby on Rails Find with date range

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
George B. (Guest)
on 2007-06-06 01:26
(Received via mailing list)
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
Robert W. (Guest)
on 2007-06-06 01:41
(Received via mailing list)
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.
Philip H. (Guest)
on 2007-06-06 01:41
(Received via mailing list)
> 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/
George B. (Guest)
on 2007-06-06 01:54
(Received via mailing list)
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. :-)

-George
George B. (Guest)
on 2007-06-06 01:55
(Received via mailing list)
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.
cammo (Guest)
on 2007-06-06 03:13
(Received via mailing list)
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
This topic is locked and can not be replied to.