Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

AR::AttributeMethods#[] raises AM::AttributeMissingError for missing att... #8056

Merged
merged 1 commit into from

2 participants

@frodsan

...ributes.

This fixes the following behaviour:

class Person < ActiveRecord::Base
  belongs_to :company
end

# Before:
person = Person.select('id').first
person[:name]       # => nil
person.name         # => ActiveModel::MissingAttributeError: missing_attribute: name
person[:company_id] # => nil
person.company      # => nil

# After:
person = Person.select('id').first
person[:name]       # => ActiveModel::MissingAttributeError: missing_attribute: name
person.name         # => ActiveModel::MissingAttributeError: missing_attribute: name
person[:company_id] # => ActiveModel::MissingAttributeError: missing_attribute: company_id
person.company      # => ActiveModel::MissingAttributeError: missing_attribute: company_id

Fixes #5433.

@frodsan frodsan AR::AttributeMethods#[] raises AM::AttributeMissingError for missing …
…attributes.

This fixes the following behaviour:

    class Person < ActiveRecord::Base
      belongs_to :company
    end

    # Before:
    person = Person.select('id').first
    person[:name]       # => nil
    person.name         # => ActiveModel::MissingAttributeError: missing_attribute: name
    person[:company_id] # => nil
    person.company      # => nil

    # After:
    person = Person.select('id').first
    person[:name]       # => ActiveModel::MissingAttributeError: missing_attribute: name
    person.name         # => ActiveModel::MissingAttributeError: missing_attribute: name
    person[:company_id] # => ActiveModel::MissingAttributeError: missing_attribute: company_id
    person.company      # => ActiveModel::MissingAttributeError: missing_attribute: company_id

Fixes #5433.
10f6f90
@frodsan

/cc @rafaelfranca Sorry for the previous PR, I think I forced push without a commit lulz.

@rafaelfranca rafaelfranca merged commit c82f0d7 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 28, 2012
  1. @frodsan

    AR::AttributeMethods#[] raises AM::AttributeMissingError for missing …

    frodsan authored
    …attributes.
    
    This fixes the following behaviour:
    
        class Person < ActiveRecord::Base
          belongs_to :company
        end
    
        # Before:
        person = Person.select('id').first
        person[:name]       # => nil
        person.name         # => ActiveModel::MissingAttributeError: missing_attribute: name
        person[:company_id] # => nil
        person.company      # => nil
    
        # After:
        person = Person.select('id').first
        person[:name]       # => ActiveModel::MissingAttributeError: missing_attribute: name
        person.name         # => ActiveModel::MissingAttributeError: missing_attribute: name
        person[:company_id] # => ActiveModel::MissingAttributeError: missing_attribute: company_id
        person.company      # => ActiveModel::MissingAttributeError: missing_attribute: company_id
    
    Fixes #5433.
This page is out of date. Refresh to see the latest.
View
25 activerecord/CHANGELOG.md
@@ -1,5 +1,28 @@
## Rails 4.0.0 (unreleased) ##
+* `ActiveRecord::AttributeMethods#[]` raises `ActiveModel::MissingAttributeError`
+ error if the given attribute is missing. Fixes #5433.
+
+ class Person < ActiveRecord::Base
+ belongs_to :company
+ end
+
+ # Before:
+ person = Person.select('id').first
+ person[:name] # => nil
+ person.name # => ActiveModel::MissingAttributeError: missing_attribute: name
+ person[:company_id] # => nil
+ person.company # => nil
+
+ # After:
+ person = Person.select('id').first
+ person[:name] # => ActiveModel::MissingAttributeError: missing_attribute: name
+ person.name # => ActiveModel::MissingAttributeError: missing_attribute: name
+ person[:company_id] # => ActiveModel::MissingAttributeError: missing_attribute: company_id
+ person.company # => ActiveModel::MissingAttributeError: missing_attribute: company_id
+
+ *Francesco Rodriguez*
+
* Small binary fields use the `VARBINARY` MySQL type, instead of `TINYBLOB`.
*Victor Costan*
@@ -51,7 +74,7 @@
*Scott Willson*
-* Fix bug where sum(expression) returns string '0' for no matching records
+* Fix bug where sum(expression) returns string '0' for no matching records.
Fixes #7439
*Tim Macfarlane*
View
10 activerecord/lib/active_record/attribute_methods.rb
@@ -266,16 +266,22 @@ def column_for_attribute(name)
# 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+.
View
6 activerecord/test/cases/attribute_methods_test.rb
@@ -287,6 +287,12 @@ def test_read_attribute
assert_equal "Don't change the topic", topic[:title]
end
+ def test_read_attribute_raises_missing_attribute_error_when_not_exists
+ computer = Computer.select('id').first
+ assert_raises(ActiveModel::MissingAttributeError) { computer[:developer] }
+ assert_raises(ActiveModel::MissingAttributeError) { computer[:extendedWarranty] }
+ end
+
def test_read_attribute_when_false
topic = topics(:first)
topic.approved = false
Something went wrong with that request. Please try again.