# Should this work? - "validates\_inclusion\_of :start\_date, :in =\> (Time.now.to\_date - 100.years)..(Tim

**URL:** <https://www.ruby-forum.com/t/should-this-work-validates-inclusion-of-start-date-in-time-now-to-date-100-years-tim/153768>\
**Category:** Rails\
**Created:** [November 20, 2008, 10:23pm UTC](https://www.ruby-forum.com/t/should-this-work-validates-inclusion-of-start-date-in-time-now-to-date-100-years-tim/153768 "2008-11-20T22:23:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [November 20, 2008, 10:23pm UTC](https://www.ruby-forum.com/t/should-this-work-validates-inclusion-of-start-date-in-time-now-to-date-100-years-tim/153768/1 "2008-11-20T22:23:50Z")

</div>

wondering if should this work? - “validates\_inclusion\_of :start\_date,  
:in =\> (Time.now.to\_date - 100.years)…(Time.now.to\_date + 100.years)”  
(doesn’t seem to for me)

i.e. should I be able to “validates\_inclusion\_of” to do date validation.

…  
class GraphOptions \< ActiveRecord::BaseWithoutTable  
column :start\_date, :date  
validates\_inclusion\_of :start\_date, :in =\> (Time.now.to\_date -  
100.years)…(Time.now.to\_date + 100.years)  
end  
…

tks

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [November 21, 2008, 8:23pm UTC](https://www.ruby-forum.com/t/should-this-work-validates-inclusion-of-start-date-in-time-now-to-date-100-years-tim/153768/2 "2008-11-21T20:23:48Z")

</div>

Greg,

validates\_inclusion\_of is looking for :in to be an enumeration. In a  
simple case (one day granularity) you’re asking for a list that’s  
365.25 \* 200 =\> 73050 items long.

My guess is it’s a good thing it didn’t work 😉

Maybe you want to go for a min/max check here.

Rick

On Nov 20, 11:22 am, “Greg H.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [November 22, 2008, 7:09am UTC](https://www.ruby-forum.com/t/should-this-work-validates-inclusion-of-start-date-in-time-now-to-date-100-years-tim/153768/3 "2008-11-22T07:09:39Z")

</div>

I’ve actually run with the following:

def validate  
errors.add(‘start\_date’, ‘is not in Date format’) if  
!start\_date.kind\_of?(Date)  
errors.add(‘end\_date’, ‘is not in Date format’) if  
!end\_date.kind\_of?(Date)  
end

Its seems that the Rails controller tries to convert it to Date and  
either succeeds or fails (without throwing an exception). So I just  
test to see at the subsequent validate stage whether there is a Date  
object or not

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [November 22, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/should-this-work-validates-inclusion-of-start-date-in-time-now-to-date-100-years-tim/153768/4 "2008-11-22T12:05:37Z")

</div>

Right, but back to your initial question, I don’t think my initial  
answer was even close to the mark.

I believe the problem you’re having is that the validates\_inclusion is  
looking for an enumeration hanging off the :in. i.e. [1,2,3,4,5] or  
1…5 will work but (My Name Is Frank)…(Your N. Is Louie) somehow  
doesn’t look right, does it?

You’re doing :in =\> (Sun, 22 Nov 1908)…(Sun, 22 Nov 2108)  
which just isn’t going to happen. Try converting to Julian days, that  
way you’ll be bounded with two positive integers.

Rick

On Nov 21, 8:08 pm, “Greg H.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [November 22, 2008, 12:30pm UTC](https://www.ruby-forum.com/t/should-this-work-validates-inclusion-of-start-date-in-time-now-to-date-100-years-tim/153768/5 "2008-11-22T12:30:49Z")

</div>

Greg - you might take a look at time\_with\_zone.rb in ActiveSupport -  
I’m looking at 2.2.0 but I know it’s in 2.1.2 as well.

a = Time.now  
a.is\_a?(Time) =\> true  
a = “My name is Fred”  
a.is\_a?(Time) =\> false

Maybe closer to what you need?

Rick

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [November 22, 2008, 2:27pm UTC](https://www.ruby-forum.com/t/should-this-work-validates-inclusion-of-start-date-in-time-now-to-date-100-years-tim/153768/6 "2008-11-22T14:27:26Z")

</div>

On 21 Nov 2008, at 19:22, Rick wrote:

> Greg,
> 
> validates\_inclusion\_of is looking for :in to be an enumeration. In a  
> simple case (one day granularity) you’re asking for a list that’s  
> 365.25 \* 200 =\> 73050 items long.

ranges are enumerable and ruby is smart enough to keep it as a range, eg

r = (-1000000000…1000000000)  
does not create a 2 billion element array

Fred

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [November 23, 2008, 12:53am UTC](https://www.ruby-forum.com/t/should-this-work-validates-inclusion-of-start-date-in-time-now-to-date-100-years-tim/153768/7 "2008-11-23T00:53:53Z")

</div>

thanks - I’m specifically looking at Dates so I might stick with the  
below that seems to be working fine…

```
errors.add('start_date', 'is not in Date format') if

```

!start\_date.kind\_of?(Date)

cheers

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [November 24, 2008, 10:06pm UTC](https://www.ruby-forum.com/t/should-this-work-validates-inclusion-of-start-date-in-time-now-to-date-100-years-tim/153768/8 "2008-11-24T22:06:05Z")

</div>

Hello Greg,

So here it is. After a couple of mail exchanges with Fred, I need to  
tell you that I was wrong from the start. Your initial code as given,

validates\_inclusion\_of :start\_date, :in =\>  
(Time.now.to\_date - 100.years)…(Time.now.to\_date + 100.years)

will work in the case where :start\_date is the name of one of your  
records fields and is a member of the Time class.

Sorry if my confused meander wasted too much of your time.

Thanks for your help Fred.

Rick

On Nov 22, 1:52 pm, “Greg H.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
