consider scenario,
User model with name, birth_date fields (here birth_date is optional)
inside view form birth_date is assigned as ’31/31/1985′ which is invalid
ideally user object should be invalid and raise and error on birth_date field but thats not happening and user object gets saved with birth_date as blank which is completely misleading.
After debugging found that while assigning attributes birth_date value it gets assigned as blank and as birth_date is optional object gets saved.
u = User.new
# invalid date assignment
u.birth_date = '31/31/2011'
u.birth_date #=> nil
# valid date assignment
u.birth_date = '1/1/2011'
u.birth_date #=> Sat, 01 Jan 2011
class User < ActiveRecord::Base
def initialize(args={})
logger.info args[:birth_date] #invalid value comes upto here but vanishes afterwords
super
end
end
I am not saying its incorrect, but it should not misleading from user point of view.
bcz after entering invalid birth_date user will assume he entered correct birth_date but actually his birth_date is not saved at all.
Any clue how to get around this issue ??
I don’t know whether the
allow_blankandallow_niloptions work with custom validations, but it’s easy to write it yourself.