# Datetime\_select, is creating an instance of the Time class

**URL:** <https://www.ruby-forum.com/t/datetime-select-is-creating-an-instance-of-the-time-class/108853>\
**Category:** Rails\
**Created:** [July 8, 2007, 4:08am UTC](https://www.ruby-forum.com/t/datetime-select-is-creating-an-instance-of-the-time-class/108853 "2007-07-08T04:08:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [July 8, 2007, 4:08am UTC](https://www.ruby-forum.com/t/datetime-select-is-creating-an-instance-of-the-time-class/108853/1 "2007-07-08T04:08:53Z")

</div>

Hello

It seems that datetime\_select, is creating an instance of the Time  
class. Why would that be?

I found this while building a site where people can create vigils  
(prayer vigils, peace vigils, etc). When I added a validation method  
that compared start\_date to created\_at, it generated the error:

```
ArgumentError (comparison of Time with DateTime failed):

```

I was able to put a breakpoint in the create action of my vigil  
controller and found that @vigil.start\_date.class returns Time type.

I’m running Rails 1.2.3 on Ruby version 1.84.20 on a Windows XP box.

The relevant code is:

View:  
\<%= datetime\_select ‘vigil’, ‘start\_date’ %\>

Vigil Controller:

def new  
s = DateTime.now  
e = s.succ  
@vigil = Vigil.new(:start\_date =\> s, :end\_date =\> e)  
end

def create  
@vigil = Vigil.new(params[:vigil])

# 

# @vigil.start\_date.class returns Time

# 

end

Model  
class Vigil \< ActiveRecord::Base  
def validate  
if start\_date \< created\_at  
errors.add(:start\_date, “must be in the future”)  
end  
end  
end

Migration for vigil table:

class CreateVigils \< ActiveRecord::Migration  
def self.up  
create\_table :vigils do |t|  
t.column :name, :string, :null =\> false  
t.column :purpose, :string, :null =\> false  
t.column :location, :string  
t.column :start\_date, :datetime, :null =\> false  
t.column :end\_date, :datetime, :null =\> false  
t.column :owner\_id, :integer  
t.column :recipient, :string  
t.column :created\_at, :datetime  
t.column :updated\_at, :datetime  
t.column :lock\_version, :integer, :default =\> 0  
end  
end

def self.down  
drop\_table :vigils  
end  
end

Your help is appreciated!

Dave

---

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [July 8, 2007, 9:00pm UTC](https://www.ruby-forum.com/t/datetime-select-is-creating-an-instance-of-the-time-class/108853/2 "2007-07-08T21:00:50Z")

</div>

Can anyone verify this for me?

Thanks,  
Dave

---

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [July 8, 2007, 10:52pm UTC](https://www.ruby-forum.com/t/datetime-select-is-creating-an-instance-of-the-time-class/108853/3 "2007-07-08T22:52:34Z")

</div>

Hey Dave,  
The time classes in Ruby are a bit confusing.

Pretty much everywhere “Time” gets used, instead of “DateTime”, and it  
works very well.

Time behaves exactly as you would expect DateTime to.  
In particular any database column defined to be “datetime” will return  
an object of the type “Time”.

So, in conclusion.

def new  
s = Time.now  
e = s.succ  
end

def create  
@vigil = Vigil.new(params[:vigil])  
success = @vigil.save  
end

# you probably also want to compare start\_date with Time.now, it seems

more relevant than “created\_at”

Model  
class Vigil \< ActiveRecord::Base  
def validate  
if start\_date \< Time.now  
errors.add(:start\_date, “must be in the future”)  
end  
end  
end

Dave Dumaresq wrote:

> Can anyone verify this for me?
> 
> Thanks,  
> Dave
