module ActiveRecord::Type
Constants
Active Model BigInteger Type
Attribute type for integers that can be serialized to an unlimited number of bytes. This type is registered under the :big_integer
key.
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 ActiveModel::Type::Integer
type.
ActiveModel::Type::BigInteger
Active Model Binary Type
Attribute type for representation of binary data. This type is registered under the :binary
key.
Non-string values are coerced to strings using their to_s
method.
ActiveModel::Type::Binary
Active Model Boolean Type
A class that behaves like a boolean type, including rules for coercion of user input.
-
"false"
,"f"
,"0"
,0
or any other value inFALSE_VALUES
will be coerced tofalse
. -
Empty strings are coerced to
nil
. -
All other values will be coerced to
true
.
ActiveModel::Type::Boolean
Active Model Decimal Type
Attribute type for decimal, high-precision floating point numeric representation. It is registered under the :decimal
key.
class BagOfCoffee
include ActiveModel::Attributes
attribute :weight, :decimal
end
Numeric instances are converted to BigDecimal
instances. Any other objects are cast using their to_d
method, except for blank strings, which are cast to nil
. If a to_d
method is not defined, the object is converted to a string using to_s
, which is then cast using to_d
.
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`)
Decimal precision defaults to 18, and can be customized when declaring an attribute:
class BagOfCoffee
include ActiveModel::Attributes
attribute :weight, :decimal, precision: 24
end
ActiveModel::Type::Decimal
Active Model Float Type
Attribute type for floating point numeric values. It is registered under the :float
key.
class BagOfCoffee
include ActiveModel::Attributes
attribute :weight, :float
end
Values are cast using their to_f
method, except for the following strings:
-
Blank strings are cast to
nil
. -
"Infinity"
is cast toFloat::INFINITY
. -
"-Infinity"
is cast to-Float::INFINITY
. -
"NaN"
is cast toFloat::NAN
.bag = BagOfCoffee.new
bag.weight = “0.25” bag.weight # => 0.25
bag.weight = “” bag.weight # => nil
bag.weight = “NaN” bag.weight # => Float::NAN
ActiveModel::Type::Float
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 to_s
method. Boolean values are treated differently, however: true
will be cast to "t"
and false
will be cast to "f"
. These strings can be customized when declaring an attribute:
class Person
include ActiveModel::Attributes
attribute :active, :immutable_string, true: "aye", false: "nay"
end
person = Person.new
person.active = true
person.active # => "aye"
ActiveModel::Type::ImmutableString
Active Model Integer Type
Attribute type for integer representation. This type is registered under the :integer
key.
class Person
include ActiveModel::Attributes
attribute :age, :integer
end
Values are cast using their to_i
method, except for blank strings, which are cast to nil
. If a to_i
method is not defined or raises an error, the value will be cast to nil
.
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)
Serialization also works under the same principle. Non-numeric strings are serialized as nil
, for example.
Serialization also validates that the integer can be stored using a limited number of bytes. If it cannot, an ActiveModel::RangeError
will be raised. The default limit is 4 bytes, and can be customized when declaring an attribute:
class Person
include ActiveModel::Attributes
attribute :age, :integer, limit: 6
end
ActiveModel::Type::Integer
Active Model String Type
Attribute type for strings. It is registered under the :string
key.
This class is a specialization of ActiveModel::Type::ImmutableString
. It performs coercion in the same way, and can be configured in the same way. However, it accounts for mutable strings, so dirty tracking can properly check if a string has changed.
ActiveModel::Type::String
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.
ActiveModel::Type::Value
Public class methods
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.
Source code GitHub
# File activerecord/lib/active_record/type.rb, line 37
def register(type_name, klass = nil, **options, &block)
registry.register(type_name, klass, **options, &block)
end
Namespace
ActiveRecord::
Type:: BigInteger ActiveRecord::
Type:: Binary ActiveRecord::
Type:: Boolean ActiveRecord::
Type:: Date ActiveRecord::
Type:: DateTime ActiveRecord::
Type:: Decimal ActiveRecord::
Type:: Float ActiveRecord::
Type:: ImmutableString ActiveRecord::
Type:: Integer ActiveRecord::
Type:: Internal ActiveRecord::
Type:: Json ActiveRecord::
Type:: String ActiveRecord::
Type:: Time ActiveRecord::
Type:: Value
Definition files
activerecord/
lib/ active_record/ type.rb activerecord/
lib/ active_record/ type/ adapter_specific_registry.rb activerecord/
lib/ active_record/ type/ date.rb activerecord/
lib/ active_record/ type/ date_time.rb activerecord/
lib/ active_record/ type/ decimal_without_scale.rb
8 More Less
activerecord/
lib/ active_record/ type/ hash_lookup_type_map.rb activerecord/
lib/ active_record/ type/ internal/ timezone.rb activerecord/
lib/ active_record/ type/ json.rb activerecord/
lib/ active_record/ type/ serialized.rb activerecord/
lib/ active_record/ type/ text.rb activerecord/
lib/ active_record/ type/ time.rb activerecord/
lib/ active_record/ type/ type_map.rb activerecord/
lib/ active_record/ type/ unsigned_integer.rb