Module: ActiveRecord::Attributes::ClassMethods
- Defined in:
- activerecord/lib/active_record/attributes.rb
Overview
Active Record Attributes
Instance Method Summary collapse
- 
  
    
      #_default_attributes  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    :nodoc:. 
- 
  
    
      #define_attribute(name, cast_type, default: NO_DEFAULT_PROVIDED, user_provided_default: true)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    This API only accepts type objects, and will do its work immediately instead of waiting for the schema to load. 
Instance Method Details
#_default_attributes ⇒ Object
:nodoc:
| 241 242 243 244 245 246 247 248 249 250 251 252 253 | # File 'activerecord/lib/active_record/attributes.rb', line 241 def _default_attributes # :nodoc: @default_attributes ||= begin attributes_hash = with_connection do |connection| columns_hash.transform_values do |column| ActiveModel::Attribute.from_database(column.name, column.default, type_for_column(connection, column)) end end attribute_set = ActiveModel::AttributeSet.new(attributes_hash) apply_pending_attribute_modifications(attribute_set) attribute_set end end | 
#define_attribute(name, cast_type, default: NO_DEFAULT_PROVIDED, user_provided_default: true) ⇒ Object
This API only accepts type objects, and will do its work immediately instead of waiting for the schema to load. While this method is provided so it can be used by plugin authors, application code should probably use ClassMethods#attribute.
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.
| 231 232 233 234 235 236 237 238 239 | # File 'activerecord/lib/active_record/attributes.rb', line 231 def define_attribute( name, cast_type, default: NO_DEFAULT_PROVIDED, user_provided_default: true ) attribute_types[name] = cast_type define_default_attribute(name, default, cast_type, from_user: user_provided_default) end |