- MODULE ActiveRecord::Type::Internal
- CLASS ActiveRecord::Type::BigInteger
- CLASS ActiveRecord::Type::Binary
- CLASS ActiveRecord::Type::Boolean
- CLASS ActiveRecord::Type::Date
- CLASS ActiveRecord::Type::DateTime
- CLASS ActiveRecord::Type::Decimal
- CLASS ActiveRecord::Type::Float
- CLASS ActiveRecord::Type::ImmutableString
- CLASS ActiveRecord::Type::Integer
- CLASS ActiveRecord::Type::Json
- CLASS ActiveRecord::Type::String
- CLASS ActiveRecord::Type::Time
- CLASS ActiveRecord::Type::Value
- R
常量
| BigInteger | = | ActiveModel::Type::BigInteger |
Active Model BigInteger 类型¶ ↑可以序列化为任意字节数的整数的属性类型。此类型在 class Person include ActiveModel::Attributes attribute :id, :big_integer end person = Person.new person.id = "18_000_000_000" person.id # => 18000000000 所有转换和序列化都与标准的 |
||
| Binary | = | ActiveModel::Type::Binary |
Active Model Binary 类型¶ ↑用于表示二进制数据的属性类型。此类型在 非字符串值使用其 |
||
| Boolean | = | ActiveModel::Type::Boolean |
Active Model Boolean 类型¶ ↑一个行为类似于布尔类型的类,包括用户输入强制转换规则。
|
||
| Decimal | = | ActiveModel::Type::Decimal |
Active Model Decimal 类型¶ ↑用于十进制、高精度浮点数表示的属性类型。它在 class BagOfCoffee include ActiveModel::Attributes attribute :weight, :decimal end
bag = BagOfCoffee.new bag.weight = 0.01 bag.weight # => 0.1e-1 bag.weight = "0.01" bag.weight # => 0.1e-1 bag.weight = "" bag.weight # => nil bag.weight = :arbitrary bag.weight # => nil (the result of `.to_s.to_d`)
class BagOfCoffee include ActiveModel::Attributes attribute :weight, :decimal, precision: 24 end |
||
| Float | = | ActiveModel::Type::Float |
Active Model Float 类型¶ ↑用于浮点数值的属性类型。它在 class BagOfCoffee include ActiveModel::Attributes attribute :weight, :float end bag = BagOfCoffee.new bag.weight = "0.25" bag.weight # => 0.25 bag.weight = "" bag.weight # => nil bag.weight = "NaN" bag.weight # => Float::NAN 除了以下字符串外,值都将使用其
|
||
| ImmutableString | = | ActiveModel::Type::ImmutableString |
Active Model ImmutableString 类型¶ ↑用于表示不可变字符串的属性类型。它会将传入的值转换为冻结字符串。 class Person include ActiveModel::Attributes attribute :name, :immutable_string end person = Person.new person.name = 1 person.name # => "1" person.name.frozen? # => true 值将使用其 class Person include ActiveModel::Attributes attribute :active, :immutable_string, true: "aye", false: "nay" end person = Person.new person.active = true person.active # => "aye" |
||
| Integer | = | ActiveModel::Type::Integer |
Active Model Integer 类型¶ ↑用于整数表示的属性类型。此类型在 class Person include ActiveModel::Attributes attribute :age, :integer end 值将使用其 person = Person.new person.age = "18" person.age # => 18 person.age = "" person.age # => nil person.age = :not_an_integer person.age # => nil (because Symbol does not define #to_i)
class Person include ActiveModel::Attributes attribute :age, :integer, limit: 6 end |
||
| 字符串 | = | ActiveModel::Type::String |
Active Model String 类型¶ ↑用于字符串的属性类型。它在 此类是 |
||
| Value | = | ActiveModel::Type::Value |
Active Model Value 类型¶ ↑所有属性类型的基类。此类也作为未指定类型的属性的默认类型。 |
||
类公共方法
register(type_name, klass = nil, **options, &block) Link
向注册表添加一个新类型,允许通过 ActiveRecord::Base.attribute 将其引用为符号。如果您的类型仅用于特定的数据库适配器,则可以通过传递 adapter: :postgresql 来实现。如果您的类型与当前适配器的本机类型同名,则会引发异常,除非您指定 :override 选项。override: true 将导致使用您的类型而不是本机类型。override: false 将导致在存在本机类型的情况下,本机类型优先于您的类型。