# Named\_scope with dates

**URL:** <https://www.ruby-forum.com/t/named-scope-with-dates/191388>\
**Category:** Rails\
**Created:** [July 11, 2010, 4:28pm UTC](https://www.ruby-forum.com/t/named-scope-with-dates/191388 "2010-07-11T16:28:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [July 11, 2010, 4:28pm UTC](https://www.ruby-forum.com/t/named-scope-with-dates/191388/1 "2010-07-11T16:28:32Z")

</div>

Hi,

I want to create a named scope that returns all the line where date is  
in X number of days.  
This is how it looks

```
named_scope :in, lambda { |number|
      { :conditions => ["event_date = ?", number.days.from_now] }
    }

```

My problem is that is compares the date with the hours, minutes and  
seconds and just would to compare the day…  
How do I do this?

Greg

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [July 12, 2010, 9:16am UTC](https://www.ruby-forum.com/t/named-scope-with-dates/191388/2 "2010-07-12T09:16:11Z")

</div>

On 11 jul, 16:28, Greg Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My problem is that is compares the date with the hours, minutes and  
> seconds and just would to compare the day…  
> How do I do this?
> 
> ## Greg
> 
> Posted viahttp://www.ruby-forum.com/.

I think this would work:

:conditions =\> [“event\_date between ? and ?”,  
number.days.from\_now.beginning\_of\_day,  
number.days.from\_now.end\_of\_day]

Regards

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [July 12, 2010, 3:51pm UTC](https://www.ruby-forum.com/t/named-scope-with-dates/191388/3 "2010-07-12T15:51:22Z")

</div>

For MySql this would work

named\_scope :in, lambda {|number|  
{:conditions =\> [“date(event\_date) = ?”,  
number.days.from\_now.to\_date]}  
}

Hope that helps.

Greg
