Module: ActiveRecord::AttributeMethods

Extended by:
ActiveSupport::Autoload, ActiveSupport::Concern
Includes:
ActiveModel::AttributeMethods
Included in:
Base
Defined in:
lib/active_record/attribute_methods.rb,
lib/active_record.rb,
lib/active_record/attribute_methods/read.rb,
lib/active_record/attribute_methods/dirty.rb,
lib/active_record/attribute_methods/query.rb,
lib/active_record/attribute_methods/write.rb,
lib/active_record/attribute_methods/primary_key.rb,
lib/active_record/attribute_methods/serialization.rb,
lib/active_record/attribute_methods/before_type_cast.rb,
lib/active_record/attribute_methods/time_zone_conversion.rb,
lib/active_record/attribute_methods/deprecated_underscore_read.rb

Overview

Active Record Attribute Methods

Defined Under Namespace

Modules: BeforeTypeCast, ClassMethods, DeprecatedUnderscoreRead, Dirty, PrimaryKey, Query, Read, Serialization, TimeZoneConversion, Write

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

If we haven’t generated any methods yet, generate them, then see if we’ve created the method we’re looking for.



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/active_record/attribute_methods.rb', line 119

def method_missing(method, *args, &block)
  unless self.class.attribute_methods_generated?
    self.class.define_attribute_methods

    if respond_to_without_attributes?(method)
      send(method, *args, &block)
    else
      super
    end
  else
    super
  end
end

Instance Method Details

#attribute_for_inspect(attr_name) ⇒ Object

Returns an #inspect-like string for the value of the attribute attr_name. String attributes are truncated upto 50 characters, and Date and Time attributes are returned in the :db format. Other attributes return the value of #inspect without modification.

person = Person.create!(:name => "David Heinemeier Hansson " * 3)

person.attribute_for_inspect(:name)
# => '"David Heinemeier Hansson David Heinemeier Hansson D..."'

person.attribute_for_inspect(:created_at)
# => '"2009-01-12 04:48:57"'


180
181
182
183
184
185
186
187
188
189
190
# File 'lib/active_record/attribute_methods.rb', line 180

def attribute_for_inspect(attr_name)
  value = read_attribute(attr_name)

  if value.is_a?(String) && value.length > 50
    "#{value[0..50]}...".inspect
  elsif value.is_a?(Date) || value.is_a?(Time)
    %("#{value.to_s(:db)}")
  else
    value.inspect
  end
end

#attribute_missing(match, *args, &block) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/active_record/attribute_methods.rb', line 133

def attribute_missing(match, *args, &block)
  if self.class.columns_hash[match.attr_name]
    ActiveSupport::Deprecation.warn(
      "The method `#{match.method_name}', matching the attribute `#{match.attr_name}' has " \
      "dispatched through method_missing. This shouldn't happen, because `#{match.attr_name}' " \
      "is a column of the table. If this error has happened through normal usage of Active " \
      "Record (rather than through your own code or external libraries), please report it as " \
      "a bug."
    )
  end

  super
end

#attribute_namesObject

Returns an array of names for the attributes available on this object.



158
159
160
# File 'lib/active_record/attribute_methods.rb', line 158

def attribute_names
  @attributes.keys
end

#attribute_present?(attribute) ⇒ Boolean

Returns true if the specified attribute has been set by the user or by a database load and is neither nil nor empty? (the latter only applies to objects that respond to empty?, most notably Strings).

Returns:

  • (Boolean)


194
195
196
197
# File 'lib/active_record/attribute_methods.rb', line 194

def attribute_present?(attribute)
  value = read_attribute(attribute)
  !value.nil? || (value.respond_to?(:empty?) && !value.empty?)
end

#attributesObject

Returns a hash of all the attributes with their names as keys and the values of the attributes as values.



163
164
165
# File 'lib/active_record/attribute_methods.rb', line 163

def attributes
  Hash[@attributes.map { |name, _| [name, read_attribute(name)] }]
end

#column_for_attribute(name) ⇒ Object

Returns the column object for the named attribute.



200
201
202
# File 'lib/active_record/attribute_methods.rb', line 200

def column_for_attribute(name)
  self.class.columns_hash[name.to_s]
end

#has_attribute?(attr_name) ⇒ Boolean

Returns true if the given attribute is in the attributes hash

Returns:

  • (Boolean)


153
154
155
# File 'lib/active_record/attribute_methods.rb', line 153

def has_attribute?(attr_name)
  @attributes.has_key?(attr_name.to_s)
end

#respond_to?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
150
# File 'lib/active_record/attribute_methods.rb', line 147

def respond_to?(name, include_private = false)
  self.class.define_attribute_methods unless self.class.attribute_methods_generated?
  super
end