跳至内容 跳至搜索

Active Model 日期类型

用于日期表示的属性类型。它以 :date 键注册。

class Person
  include ActiveModel::Attributes

  attribute :birthday, :date
end

person = Person.new
person.birthday = "1989-07-13"

person.birthday.class # => Date
person.birthday.year  # => 1989
person.birthday.month # => 7
person.birthday.day   # => 13

String 值使用 ISO 8601 日期格式解析。任何其他值都使用其 to_date 方法进行转换,如果存在的话。

方法
T
包含的模块

常量

ISO_DATE = /\A(\d{4})-(\d\d)-(\d\d)\z/
 

实例公共方法

type()

# File activemodel/lib/active_model/type/date.rb, line 31
def type
  :date
end

type_cast_for_schema(value)

# File activemodel/lib/active_model/type/date.rb, line 35
def type_cast_for_schema(value)
  value.to_fs(:db).inspect
end