Rails慢性宝石未按预期验证

时间:2012-11-09 04:38:15

标签: ruby-on-rails ruby ruby-on-rails-3 chronic

我正在尝试将日期'03 / 20/1985'输入名为“birthday”的文本字段,并将其插入到数据库字段中,其列类型为“date”。

当我输入10/20/1985时,我收到错误“生日无效”,但是当我输入20/10/1985时,它运行正常。

从我一直在阅读的所有文档中,慢性应该将'10 / 20/1985'解析为mm / dd / yyyy,但似乎它将其解析为dd / mm / yyyy。

如何将日期解析为mm / dd / yyyy?

/models/user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:login]

  # Virtual attribute for authenticating by either username or email
  # This is in addition to a real persisted field like 'username'
  attr_accessor :login

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :login, :first_name, :last_name, :home_phone, :cell_phone, :work_phone, :birthday, :home_address, :work_address, :position, :company

  validate :birthday_is_date
  validate :position, :presence => true

  require 'chronic'

  # validate the birthday format
  def birthday_is_date 
    errors.add(:birthday, "is invalid") unless Chronic.parse(birthday)
  end

  # validates email or username when logging in
  def self.find_first_by_auth_conditions(warden_conditions)
    conditions = warden_conditions.dup
    if login = conditions.delete(:login)
      where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
    else
      where(conditions).first
    end
  end

end

2 个答案:

答案 0 :(得分:3)

如果该值作为日期存储在数据库中,则Rails会在赋值时将字符串中的值强制转换为Ruby Date。我认为它可能使用内置的Date.parse方法(docs):

Date.parse "2012-10-20"
# => #<Date 2012-10-20 ...>

Date.parse "20-10-2012"
# => #<Date 2012-10-20 ...>

Date.parse "10-20-2012"
# => ArgumentError: invalid date

在这种情况下,您希望避免强制并获取原始字符串以解析Chronic。这是virtual attributes的理想用例。有几种方法可以做到这一点,这样的事情应该让你开始

class User < ActiveRecord::Base
  validate :birthday_is_date

  # an explicit implementation
  def birthday_string
    if @birthday_string
      @birthday_string
    elsif birthday.present?
      birthday.strftime("%d-%m-%Y")
    else
      ""
    end
  end

  # a shorter implementation
  def birthday_string
    @birthday_string || birthday.try(:strftime, "%d-%m-%Y")
  end

  def birthday_string=(value)
    @birthday_string = value
    self.birthday = parse_birthday
  end

  private

  def birthday_is_date
    errors.add(:birthday_string, "is invalid") unless parse_birthday
  end

  def parse_birthday
    Chronic.parse(birthday_string)
  end
end

然后在表单中使用birthday_string,而不是birthday

答案 1 :(得分:1)

两者都适合我,也许你需要更新。

此外:

Chronic.parse '2/10/1985'
#=> 1985-02-10 12:00:00 +0800
Chronic.parse '2/10/1985', :endian_precedence => :little
#=> 1985-10-02 12:00:00 +0800