Permalink
Browse files

Add more data to AR::UnknownAttributeError

begin
  Topic.new("hello" => "world")
rescue ActiveRecord::UnknownAttributeError => e
  e.record # => #<Topic ... >
  e.attribute # => "hello"
end
  • Loading branch information...
1 parent e38c93a commit 711097e6a5af61a31a0547223038a4b5e1d59366 @bogdan bogdan committed with tenderlove May 13, 2013
@@ -45,7 +45,7 @@ def _assign_attribute(k, v)
if respond_to?("#{k}=")
raise
else
- raise UnknownAttributeError, "unknown attribute: #{k}"
+ raise UnknownAttributeError.new(self, k)
end
end
@@ -159,6 +159,15 @@ class DangerousAttributeError < ActiveRecordError
# 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
@@ -711,6 +711,18 @@ def test_bulk_update_respects_access_control
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
@zenspider
zenspider May 16, 2013

This should use assert_raises.

@bogdan
bogdan May 17, 2013

This is checked one test above.

+ 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)

0 comments on commit 711097e

Please sign in to comment.