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