# Database Agnostic Date Functions

**URL:** <https://www.ruby-forum.com/t/database-agnostic-date-functions/123314>\
**Category:** Rails\
**Created:** [December 16, 2007, 8:58am UTC](https://www.ruby-forum.com/t/database-agnostic-date-functions/123314 "2007-12-16T08:58:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![partydrone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@partydrone](https://www.ruby-forum.com/u/partydrone)\
**Post date:** [December 16, 2007, 8:58am UTC](https://www.ruby-forum.com/t/database-agnostic-date-functions/123314/1 "2007-12-16T08:58:56Z")

</div>

I have a model for events with a start date and an end date. I want to  
find all events for the current month. How would I go about that,  
exactly?

Thanks.

---

<div class="post-metadata">

**Author:** ![partydrone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@partydrone](https://www.ruby-forum.com/u/partydrone)\
**Post date:** [December 16, 2007, 3:23pm UTC](https://www.ruby-forum.com/t/database-agnostic-date-functions/123314/2 "2007-12-16T15:23:04Z")

</div>

On Sat, Dec 15, 2007 at 11:58:36PM -0800, partydrone wrote:

> I have a model for events with a start date and an end date. I want to  
> find all events for the current month. How would I go about that,  
> exactly?

You could mean any one of three things: events starting in a particular  
month, events ending in that month, or events entirely contained in a  
particular month. I’m assuming you mean events starting in a particular  
month; the conditions will be slightly different for the other options,  
and  
those differences are left as an exercise for the reader (i.e. don’t ask  
me  
to write your code for you when I’ve shown you how to go about it).

class Event \< ActiveRecord::Base

def self.find\_for\_month(date = Date.today)  
find :all, :conditions =\> [ ‘start\_date BETWEEN ? AND ?’,  
\*month\_range(date) ]  
end

# stuff…

private

def month\_range(date = Date.today)  
first = date - (date.day - 1)  
last = first + 32  
last -= last.day  
[first, last]  
end

end

> Thanks.  
> –Greg

---

<div class="post-metadata">

**Author:** ![partydrone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@partydrone](https://www.ruby-forum.com/u/partydrone)\
**Post date:** [December 17, 2007, 2:45pm UTC](https://www.ruby-forum.com/t/database-agnostic-date-functions/123314/3 "2007-12-17T14:45:17Z")

</div>

On Sun, Dec 16, 2007 at 11:41:49AM -0800, partydrone wrote:

> Thanks for your response, Greg.
> 
> Here’s what I tried doing:
> 
> class Event \< ActiveRecord::Base
> 
> def self.find\_for\_month(date = Date.today)  
> find(:all, :conditions =\> [‘start\_date BETWEEN ? AND ? OR end\_date  
> BETWEEN ? AND ?’, \*month\_range(date)], :order =\> ‘start\_date ASC’)  
> end

This is wrong, first of all, because you are referring to four variables  
to  
substitute in the conditions clause, but only providing two.

> When I test it in script/console, though, I get an error:
> 
> NoMethodError: undefined method `month\_range’ for Reservation:Class  
> […]

This is wrong, and it’s my fault. The find\_for\_month method is a class  
method, whereas the month\_range method is an instance method. That’s why  
it  
isn’t being found.

–Greg

---

<div class="post-metadata">

**Author:** ![partydrone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@partydrone](https://www.ruby-forum.com/u/partydrone)\
**Post date:** [December 17, 2007, 2:55pm UTC](https://www.ruby-forum.com/t/database-agnostic-date-functions/123314/4 "2007-12-17T14:55:19Z")

</div>

> This is wrong, first of all, because you are referring to four variables to  
> substitute in the conditions clause, but only providing two.

I tried it and it seems to be working fine. I read in the Ruby docs  
that:

[1, 2] \* 2

returns:

[1, 2, 1, 2]

So using [first, last] \* 2 actually returns four variables, not just  
the two.

Thanks a TON for your help. Much appreciated.

Andy

---

<div class="post-metadata">

**Author:** ![partydrone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@partydrone](https://www.ruby-forum.com/u/partydrone)\
**Post date:** [December 17, 2007, 3:01pm UTC](https://www.ruby-forum.com/t/database-agnostic-date-functions/123314/5 "2007-12-17T15:01:10Z")

</div>

On Sun, Dec 16, 2007 at 12:52:39PM -0800, partydrone wrote:

> [1, 2, 1, 2]
> 
> So using [first, last] \* 2 actually returns four variables, not just  
> the two.

Ah, sorry, I missed the \*2.

> Thanks a TON for your help. Much appreciated.

Sure.

> Andy  
> –Greg
