instance method define_attribute

Ruby on Rails edge

Since v5.0.7.2

Available in: v5.0.7.2 v5.1.7 v5.2.8.1 v6.0.6.1 v6.1.7.10 v7.0.10 v7.1.6 v7.2.4 v8.0.5.1 v8.1.4 edge

Signature

define_attribute( name, cast_type, default: (no_default = true), user_provided_default: true )

This API only accepts type objects. While this method is provided so it can be used by plugin authors, application code should probably use ClassMethods#attribute.

Parameters
name

The name of the attribute being defined. Expected to be a String.

cast_type

The type object to use for this attribute.

default

The default value to use when no value is provided. If this option is not passed, the previous default value (if any) will be used. Otherwise, the default will be nil. A proc can also be passed, and will be called once each time a new value is needed.

user_provided_default

Whether the default value should be cast using cast or deserialize.

Parameters

name req
cast_type req
default key = (no_default = true)
user_provided_default key = true
Source
# File activerecord/lib/active_record/attributes.rb, line 259
      def define_attribute(
        name,
        cast_type,
        default: (no_default = true),
        user_provided_default: true
      )
        add_pending_attribute_modification(PendingType.new(name, cast_type))

        unless no_default
          if user_provided_default
            add_pending_attribute_modification(PendingDefault.new(name, default))
          else
            add_pending_attribute_modification(PendingDatabaseDefault.new(name, default))
          end
        end

        reset_default_attributes
      end

Defined in activerecord/lib/active_record/attributes.rb line 259 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::Attributes::ClassMethods

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close