module Attributes
Ruby on Rails 7.2.3
Since v5.2.8.1Active Model Attributes
The Attributes module allows models to define attributes beyond simple Ruby readers and writers. Similar to Active Record attributes, which are typically inferred from the database schema, Active Model Attributes are aware of data types, can have default values, and can handle casting and serialization.
To use Attributes, include the module in your model class and define your attributes using the attribute macro. It accepts a name, a type, a default value, and any other options supported by the attribute type.
Examples
class Person
include ActiveModel::Attributes
attribute :name, :string
attribute :active, :boolean, default: true
end
person = Person.new
person.name = "Volmer"
person.name # => "Volmer"
person.active # => true
Namespace
Modules
Includes
Extends
Methods (defined here)
Private methods (3)
Show private methods Implementation detail — not part of the public API.
Methods (inherited)
From ActiveModel::AttributeMethods
(4)
From ActiveSupport::Concern
(3)
- # class_methods
- # included
- # prepended