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.



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/active_record/attribute_methods.rb', line 139

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"'


202
203
204
205
206
207
208
209
210
211
212
# File 'lib/active_record/attribute_methods.rb', line 202

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



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/active_record/attribute_methods.rb', line 153

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.



178
179
180
# File 'lib/active_record/attribute_methods.rb', line 178

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)


216
217
218
219
# File 'lib/active_record/attribute_methods.rb', line 216

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.



183
184
185
186
187
# File 'lib/active_record/attribute_methods.rb', line 183

def attributes
  attrs = {}
  attribute_names.each { |name| attrs[name] = read_attribute(name) }
  attrs
end

#column_for_attribute(name) ⇒ Object

Returns the column object for the named attribute.



222
223
224
# File 'lib/active_record/attribute_methods.rb', line 222

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)


173
174
175
# File 'lib/active_record/attribute_methods.rb', line 173

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

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

Returns:

  • (Boolean)


167
168
169
170
# File 'lib/active_record/attribute_methods.rb', line 167

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