# Active Record Question

**URL:** <https://www.ruby-forum.com/t/active-record-question/131174>\
**Category:** Rails\
**Created:** [March 6, 2008, 5:46pm UTC](https://www.ruby-forum.com/t/active-record-question/131174 "2008-03-06T17:46:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 6, 2008, 5:46pm UTC](https://www.ruby-forum.com/t/active-record-question/131174/1 "2008-03-06T17:46:45Z")

</div>

Hi-

Say I am returning some collection of DateTime items and I only want  
to compare the month. Is there a way to do this directly in the find?

Something like this…  
@coll = Collection.find(:all, :conditions =\> [“date.month  
= ?”,Time.now.lat\_month])

Thanks!

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 6, 2008, 5:53pm UTC](https://www.ruby-forum.com/t/active-record-question/131174/2 "2008-03-06T17:53:54Z")

</div>

Perfect, thanks!

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 6, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/active-record-question/131174/3 "2008-03-06T17:51:33Z")

</div>

> Say I am returning some collection of DateTime items and I only want  
> to compare the month. Is there a way to do this directly in the find?
> 
> Something like this…  
> @coll = Collection.find(:all, :conditions =\> [“date.month  
> = ?”,Time.now.lat\_month])

Well, if it’s mysql you could do this:

:conditions =\> [“MONTH(date.month) = MONTH(?)”, Time.now.last\_month]

Note this will pull up _every_ date with february as the month  
regardless  
of the year, not just feb 08.

May lose some database independence, but it will work. Just looks up  
the  
method for your particular database.

You could also change it to look for a date BETWEEN this AND that which  
all Db’s support.

-philip

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 6, 2008, 6:43pm UTC](https://www.ruby-forum.com/t/active-record-question/131174/4 "2008-03-06T18:43:05Z")

</div>

I like the range, so for simplicity, I did this:

beginLastMonth = Time.now.last\_month.beginning\_of\_month  
endLastMonth = Time.now.last\_month.at\_end\_of\_month

@col = Collection.find(:all, :conditions =\> [“start\_time \>= ? and  
start\_time \<= ?”,beginLastMonth,endLastMonth])

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 6, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/active-record-question/131174/5 "2008-03-06T18:47:53Z")

</div>

Just for my own style I’d have done:  
beginLastMonth = Time.now.last\_month.beginning\_of\_month  
endLastMonth = beginLastMonth.at\_end\_of\_month

Not only does it avoid figuring out last\_month a second time, but in  
that tiny fraction of a second between the two lines the month could  
change and then you have a two month range.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
