diff options
author | Bogdan Gusiev <agresso@gmail.com> | 2013-05-13 10:54:59 +0300 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-05-16 13:31:35 -0700 |
commit | 711097e6a5af61a31a0547223038a4b5e1d59366 (patch) | |
tree | 32ba32fbfb99662a4f14a889846f1500f3f31c90 /activerecord | |
parent | e38c93a120de84ee5c2f22e9da942233a7edd22e (diff) | |
download | rails-711097e6a5af61a31a0547223038a4b5e1d59366.tar.gz rails-711097e6a5af61a31a0547223038a4b5e1d59366.tar.bz2 rails-711097e6a5af61a31a0547223038a4b5e1d59366.zip |
Add more data to AR::UnknownAttributeError
begin
Topic.new("hello" => "world")
rescue ActiveRecord::UnknownAttributeError => e
e.record # => #<Topic ... >
e.attribute # => "hello"
end
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/attribute_assignment.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/errors.rb | 9 | ||||
-rw-r--r-- | activerecord/test/cases/attribute_methods_test.rb | 12 |
3 files changed, 22 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/attribute_assignment.rb b/activerecord/lib/active_record/attribute_assignment.rb index a13bb9299a..75377bba57 100644 --- a/activerecord/lib/active_record/attribute_assignment.rb +++ b/activerecord/lib/active_record/attribute_assignment.rb @@ -45,7 +45,7 @@ module ActiveRecord if respond_to?("#{k}=") raise else - raise UnknownAttributeError, "unknown attribute: #{k}" + raise UnknownAttributeError.new(self, k) end end diff --git a/activerecord/lib/active_record/errors.rb b/activerecord/lib/active_record/errors.rb index cd31147414..017b8bace6 100644 --- a/activerecord/lib/active_record/errors.rb +++ b/activerecord/lib/active_record/errors.rb @@ -159,6 +159,15 @@ module ActiveRecord # Raised when unknown attributes are supplied via mass assignment. class UnknownAttributeError < NoMethodError + + attr_reader :record, :attribute + + def initialize(record, attribute) + @record = record + @attribute = attribute.to_s + super("unknown attribute: #{attribute}") + end + end # Raised when an error occurred while doing a mass assignment to an attribute through the diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index a181c513ae..d882d40ecb 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -711,6 +711,18 @@ class AttributeMethodsTest < ActiveRecord::TestCase assert_raise(ActiveRecord::UnknownAttributeError) { @target.new.attributes = { :title => "Ants in pants" } } end + def test_bulk_update_raise_unknown_attribute_errro + error = nil + begin + @target.new(:hello => "world") + rescue ActiveRecord::UnknownAttributeError => error + end + assert error + assert @target, error.record + assert "hello", error.attribute + assert "unknown attribute: hello", error.message + end + def test_read_attribute_overwrites_private_method_not_considered_implemented # simulate a model with a db column that shares its name an inherited # private method (e.g. Object#system) |