跳至內容 跳至搜尋

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 30
def type
  :date
end

type_cast_for_schema(value)

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