ActiveRecord: abort datetime parsing if value is invalid

*爱你&永不变心* 提交于 2020-01-04 06:20:39

问题


I have a datetime field in my ActiveRecord model (in Rails) and I need to protect my model from crashes when date fieid is out of ranges.

I found this question and and tried to make similarly:

class Something < ActiveRecord::Base
    validate :important_date_is_valid_datetime, on: :create
    validates :important_date, presence: true

private

    def important_date_is_valid_datetime
        if (DateTime.parse(important_date.to_s()) rescue ArgumentError) == ArgumentError then
            errors.add(:important_date, 'must be a valid datetime') 
        end
    end

end 

This code can 'validate' datetime field but it don't abort field parsing if valie is out of range:

irb(main):007:0> Something.new(important_date: "2015-20-40").valid?
ArgumentError: argument out of range
    from /home/shau-kote/.gem/ruby/2.1.0/gems/activesupport-4.2.0/lib/active_support/values/time_zone.rb:350:in `initialize'
    from /home/shau-kote/.gem/ruby/2.1.0/gems/activesupport-4.2.0/lib/active_support/values/time_zone.rb:350:in `new'
    from /home/shau-kote/.gem/ruby/2.1.0/gems/activesupport-4.2.0/lib/active_support/values/time_zone.rb:350:in `parse'
    ...

Can I prevent similar crashes?


回答1:


This is an issue with Rails 4 where the exception gets thrown before your validators are even called. There is a pull request that brings back the behavior of Rails 3 where a invalid date is just nil. This pull request seems to be merged, so maybe updating your Rails version can already fix the problem.

For the meantime I can think of two workarounds: First, you could use client side validations that of course can be tampered with if wanted but would protect users using your app in "good faith" and second, instead of using a validation you could be checking for a valid date (or the ArgumentError respectively) in the controller before the parameter is used to set the datetime attribute. Both options are not optimal but should prevent more crashes.



来源:https://stackoverflow.com/questions/27789968/activerecord-abort-datetime-parsing-if-value-is-invalid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!