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''
%></p>
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Can anyone verify this for me? Thanks, Dave On Jul 7, 7:08 pm, Dave <dfdumar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello > > It seems thatdatetime_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'' %></p> > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Matthew Rudy
2007-Jul-08 20:52 UTC
Re: datetime_select, is creating an instance of the Time cla
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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---