날짜 기록 필드가 내 ActiveRecord 모델 (레일)에 있고 date fieid가 범위를 벗어 났을 때 내 모델을 충돌로부터 보호해야합니다.ActiveRecord : 값이 유효하지 않은 경우 datetime 구문 분석을 중단합니다.
나는 this 질문을 발견하고 유사하게 만들려고 :
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'
...
: 또한 Valie이 범위를 벗어날 경우
가
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
이 코드는하지만 필드 구문 분석을 '검증'날짜 필드를 중단하지 않는 수
비슷한 충돌을 예방할 수 있습니까?
필자에게는 최신 보석 레일 4.2.0이 있으며 지금까지이 문제가 있습니다. 매우 불쾌한 일이지만 "두 번째 해결 방법"은 제 프로젝트에 아주 잘 들어 맞습니다. 고맙습니다. –