Skip to content

Commit

Permalink
Revert "Revert "Raise UnknownAttributeError when unknown attributes a…
Browse files Browse the repository at this point in the history
…re supplied via mass assignment""

This reverts commit 41efd73.
  • Loading branch information
jeremy committed Sep 8, 2008
1 parent 7e6cda1 commit 4f68752
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 9 additions & 1 deletion activerecord/lib/active_record/base.rb
Expand Up @@ -122,6 +122,10 @@ class DangerousAttributeError < ActiveRecordError
class MissingAttributeError < NoMethodError
end

# Raised when unknown attributes are supplied via mass assignment.
class UnknownAttributeError < NoMethodError
end

# Raised when an error occurred while doing a mass assignment to an attribute through the
# <tt>attributes=</tt> method. The exception has an +attribute+ property that is the name of the
# offending attribute.
Expand Down Expand Up @@ -2437,7 +2441,11 @@ def attributes=(new_attributes, guard_protected_attributes = true)
attributes = remove_attributes_protected_from_mass_assignment(attributes) if guard_protected_attributes

attributes.each do |k, v|
k.include?("(") ? multi_parameter_attributes << [ k, v ] : send(k + "=", v)
if k.include?("(")
multi_parameter_attributes << [ k, v ]
else
respond_to?(:"#{k}=") ? send(:"#{k}=", v) : raise(UnknownAttributeError, "unknown attribute: #{k}")
end
end

assign_multiparameter_attributes(multi_parameter_attributes)
Expand Down
8 changes: 8 additions & 0 deletions activerecord/test/cases/base_test.rb
Expand Up @@ -904,6 +904,14 @@ def test_customized_primary_key_remains_protected_when_referred_to_as_id
assert_nil keyboard.id
end

def test_mass_assigning_invalid_attribute
firm = Firm.new

assert_raises(ActiveRecord::UnknownAttributeError) do
firm.attributes = { "id" => 5, "type" => "Client", "i_dont_even_exist" => 20 }
end
end

def test_mass_assignment_protection_on_defaults
firm = Firm.new
firm.attributes = { "id" => 5, "type" => "Client" }
Expand Down

0 comments on commit 4f68752

Please sign in to comment.