- 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
Constants
BigInteger | = | ActiveModel::Type::BigInteger |
Active Model BigInteger Type¶ ↑Attribute type for integers that can be serialized to an unlimited number of bytes. This type is registered under the class Person include ActiveModel::Attributes attribute :id, :big_integer end person = Person.new person.id = "18_000_000_000" person.id # => 18000000000 All casting and serialization are performed in the same way as the standard |
||
Binary | = | ActiveModel::Type::Binary |
Active Model Binary Type¶ ↑Attribute type for representation of binary data. This type is registered under the Non-string values are coerced to strings using their |
||
Boolean | = | ActiveModel::Type::Boolean |
Active Model Boolean Type¶ ↑A class that behaves like a boolean type, including rules for coercion of user input.
|
||
Decimal | = | ActiveModel::Type::Decimal |
Active Model Decimal Type¶ ↑Attribute type for decimal, high-precision floating point numeric representation. It is registered under the 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 Type¶ ↑Attribute type for floating point numeric values. It is registered under the 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 Values are cast using their
|
||
ImmutableString | = | ActiveModel::Type::ImmutableString |
Active Model ImmutableString Type¶ ↑Attribute type to represent immutable strings. It casts incoming values to frozen strings. class Person include ActiveModel::Attributes attribute :name, :immutable_string end person = Person.new person.name = 1 person.name # => "1" person.name.frozen? # => true Values are coerced to strings using their 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 Type¶ ↑Attribute type for integer representation. This type is registered under the class Person include ActiveModel::Attributes attribute :age, :integer end Values are cast using their 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 |
||
String | = | ActiveModel::Type::String |
Active Model String Type¶ ↑Attribute type for strings. It is registered under the This class is a specialization of |
||
Value | = | ActiveModel::Type::Value |
Active Model Value Type¶ ↑The base class for all attribute types. This class also serves as the default type for attributes that do not specify a type. |
Class Public methods
register(type_name, klass = nil, **options, &block) Link
Add a new type to the registry, allowing it to be referenced as a symbol by ActiveRecord::Base.attribute. If your type is only meant to be used with a specific database adapter, you can do so by passing adapter: :postgresql
. If your type has the same name as a native type for the current adapter, an exception will be raised unless you specify an :override
option. override: true
will cause your type to be used instead of the native type. override: false
will cause the native type to be used over yours if one exists.