Incorrect error

Posted by jspooner on Stack Overflow See other posts from Stack Overflow or by jspooner
Published on 2010-05-27T18:49:37Z Indexed on 2010/05/27 18:51 UTC
Read the original article Hit count: 217

If you assign an invalid date (like December 39th) to a datetime column ActiveRecord returns a "can't be blank" error when is should probably return an error like "Not a valid date"

My question. Is this expected rails behavior, a bug or, something that I could patch?

class ExerciseLog < ActiveRecord::Base
  validates_presence_of :scheduled_datetime
end

Fire up the console.

e = Log.new
# lets set a date for Dec 39th which obviously doesn't exist
e.scheduled_datetime = "2010-12-39"
e.save
=> false
# this is the confusing message since our form did post a valid date
e.errors.on(:scheduled_datetime)
=> "can't be blank"
e.scheduled_datetime = "2010-12-30"
e.save
=> true

I discovered this issue when I accidentally transposed the month and day values.

btw This is in Rails 2.3.5

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about datetime