module
Importance_2
Ruby on Rails latest stable (v7.1.3.2) - 0 notes

Active 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
Show files where this module is defined (1 file)
Register or log in to add new notes.