diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-10-28 12:22:24 -0700 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-10-28 12:22:24 -0700 |
commit | c82f0d76e4cfc9a80fe9988f5d1ca6f6a5594eed (patch) | |
tree | ef55c28c9ed1238840a2f3af1b166b3ec4ca51e9 /activerecord/lib | |
parent | e46a8d89baa5b58f19cd84cf9a57beb4507e7d9c (diff) | |
parent | 10f6f90d9d1bbc9598bffea90752fc6bd76904cd (diff) | |
download | rails-c82f0d76e4cfc9a80fe9988f5d1ca6f6a5594eed.tar.gz rails-c82f0d76e4cfc9a80fe9988f5d1ca6f6a5594eed.tar.bz2 rails-c82f0d76e4cfc9a80fe9988f5d1ca6f6a5594eed.zip |
Merge pull request #8056 from frodsan/fix_issue_missing_attribute
AR::AttributeMethods#[] raises AM::AttributeMissingError for missing attributes
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/attribute_methods.rb | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 9a4fcdfda3..101c641877 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -266,16 +266,22 @@ module ActiveRecord # Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example, # "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)). - # (Alias for the protected <tt>read_attribute</tt> method). + # (Alias for the protected <tt>read_attribute</tt> method). It raises an <tt>ActiveModel::MissingAttributeError</tt> + # error if the identified attribute is missing. # # 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 def [](attr_name) - read_attribute(attr_name) + read_attribute(attr_name) { |n| missing_attribute(n, caller) } end # Updates the attribute identified by <tt>attr_name</tt> with the specified +value+. |