- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1 (0)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (38)
- 7.1.3.4 (0)
- What's this?
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
Constants
DEFAULT_LIMIT = 4
Attributes
[R] | range |