instance method
[]
Ruby on Rails 8.0.4
Since v4.0.13Signature
[](attr_name)
Returns the value of the attribute identified by attr_name after it has been type cast. (For information about specific type casting behavior, see the types under ActiveModel::Type.)
class Person < ActiveRecord::Base belongs_to :organization end person = Person.new(name: "Francesco", date_of_birth: "2004-12-12") person[:name] # => "Francesco" person[:date_of_birth] # => Date.new(2004, 12, 12) person[:organization_id] # => nil
Raises ActiveModel::MissingAttributeError if the attribute is missing. Note, however, that the id attribute will never be considered missing.
person = Person.select(:name).first person[:name] # => "Francesco" person[:date_of_birth] # => ActiveModel::MissingAttributeError: missing attribute 'date_of_birth' for Person person[:organization_id] # => ActiveModel::MissingAttributeError: missing attribute 'organization_id' for Person person[:id] # => nil
Parameters
-
attr_namereq
Source
# File activerecord/lib/active_record/attribute_methods.rb, line 415
def [](attr_name)
read_attribute(attr_name) { |n| missing_attribute(n, caller) }
end
Defined in activerecord/lib/active_record/attribute_methods.rb line 415
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::AttributeMethods