# Searching by date

**URL:** <https://www.ruby-forum.com/t/searching-by-date/69031>\
**Category:** Rails\
**Created:** [September 18, 2006, 9:37am UTC](https://www.ruby-forum.com/t/searching-by-date/69031 "2006-09-18T09:37:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Naga\_harish\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90ced4/32.png) [@Naga\_harish\_K](https://www.ruby-forum.com/u/Naga_harish_K)\
**Post date:** [September 18, 2006, 9:37am UTC](https://www.ruby-forum.com/t/searching-by-date/69031/1 "2006-09-18T09:37:44Z")

</div>

Hi,  
Could anybody please help me to search the record by date

I have one products table. In that table I have created\_at.I want to  
search the records by date.

I mean, if i select month as january and year as 2006 then i want to get  
the list as per that month only.  
Ex:  
Created At Product Price  
2006-09-18 Ruby 4.5  
2006-09-18 Ruby 4.5  
2006-08-18 Ruby 4.5  
2006-08-04 shq 8.0

If i select September then I should be able to see

Created At Product Price  
2006-09-18 Ruby 4.5  
2006-09-18 Ruby 4.5

So how to do that code?? please help me regarding this…

Thanks in advance  
Harish

---

<div class="post-metadata">

**Author:** ![Naga\_harish\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90ced4/32.png) [@Naga\_harish\_K](https://www.ruby-forum.com/u/Naga_harish_K)\
**Post date:** [September 18, 2006, 10:11pm UTC](https://www.ruby-forum.com/t/searching-by-date/69031/2 "2006-09-18T22:11:02Z")

</div>

For the ruby side you can locate the month using the month() method on  
your date variable. For the SQL side look in the reference for  
BETWEEN. It’s commonly associated with date range searching.

---

<div class="post-metadata">

**Author:** ![Naga\_harish\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90ced4/32.png) [@Naga\_harish\_K](https://www.ruby-forum.com/u/Naga_harish_K)\
**Post date:** [September 18, 2006, 10:29pm UTC](https://www.ruby-forum.com/t/searching-by-date/69031/3 "2006-09-18T22:29:03Z")

</div>

And/or, see if your db supplies a way to extract a month from a date,  
then select on that.

For example, you could do something like the following in mySql.

def whatever(month)  
@products = Product.find(:all, :conditions =\> [‘month(created\_on) =  
?’, month]  
end

You get the idea.

On 9/18/06, Brittain [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I have one products table. In that table I have created\_at.I want to
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
The best way to implement code quickly is to implement less of it.  
([http://www.xprogramming.com/Practices/PracNotNeed.html](http://www.xprogramming.com/Practices/PracNotNeed.html)) – Ron  
Jeffries

---

<div class="post-metadata">

**Author:** ![Naga\_harish\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90ced4/32.png) [@Naga\_harish\_K](https://www.ruby-forum.com/u/Naga_harish_K)\
**Post date:** [September 19, 2006, 4:11pm UTC](https://www.ruby-forum.com/t/searching-by-date/69031/4 "2006-09-19T16:11:04Z")

</div>

On 9/19/06, Naga harish Kanegolla [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > For example, you could do something like the following in mySql.
> > 
> > def whatever(month)  
> > @products = Product.find(:all, :conditions =\> [‘month(created\_on) =  
> > ?’, month]  
> > end

Often that means you have a recursive infinite loop somewhere. I’d  
have to see the exact code though.

–  
A wise man once told me it is not wise to quote what a wise man once  
told you. – Unknown

---

<div class="post-metadata">

**Author:** ![Naga\_harish\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90ced4/32.png) [@Naga\_harish\_K](https://www.ruby-forum.com/u/Naga_harish_K)\
**Post date:** [September 19, 2006, 8:38am UTC](https://www.ruby-forum.com/t/searching-by-date/69031/5 "2006-09-19T08:38:39Z")

</div>

Hi,  
I tries like this. Its giving error. “stack level too deep”. wat is  
meant by this error?? could u please help?

Harish

Michael C. wrote:

> And/or, see if your db supplies a way to extract a month from a date,  
> then select on that.
> 
> For example, you could do something like the following in mySql.
> 
> def whatever(month)  
> @products = Product.find(:all, :conditions =\> [‘month(created\_on) =  
> ?’, month]  
> end
> 
> You get the idea.
> 
> On 9/18/06, Brittain [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > I have one products table. In that table I have created\_at.I want to
> > > 
> > > –  
> > > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > >
> 
> –  
> The best way to implement code quickly is to implement less of it.  
> ([http://www.xprogramming.com/Practices/PracNotNeed.html](http://www.xprogramming.com/Practices/PracNotNeed.html)) – Ron  
> Jeffries
