instance method
[]
Ruby on Rails 6.0.6
Since v4.0.13Signature
[](attr_name)
Returns the value of the attribute identified by attr_name after it has been typecast (for example, “2004-12-12” in a date column is cast to a date object, like Date.new(2004, 12, 12)). It raises ActiveModel::MissingAttributeError if the identified attribute is missing.
Note: :id is always present.
class Person < ActiveRecord::Base belongs_to :organization end person = Person.new(name: 'Francesco', age: '22') person[:name] # => "Francesco" person[:age] # => 22 person = Person.select('id').first person[:name] # => ActiveModel::MissingAttributeError: missing attribute: name person[:organization_id] # => ActiveModel::MissingAttributeError: missing attribute: organization_id
Parameters
-
attr_namereq
Source
# File activerecord/lib/active_record/attribute_methods.rb, line 322
def [](attr_name)
read_attribute(attr_name) { |n| missing_attribute(n, caller) }
end
Defined in activerecord/lib/active_record/attribute_methods.rb line 322
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::AttributeMethods