aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md5
-rw-r--r--activerecord/lib/active_record/persistence.rb8
-rw-r--r--activerecord/test/cases/attribute_methods_test.rb6
3 files changed, 15 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index d4f6e6ae48..9d80bbe51f 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,8 @@
+* `update_columns` now correctly raises `ActiveModel::MissingAttributeError`
+ if the attribute does not exist.
+
+ *Sean Griffin*
+
* Add support for hash and url configs in database hash of `ActiveRecord::Base.connected_to`.
````
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb
index 45ceb1e3ad..f991a3076e 100644
--- a/activerecord/lib/active_record/persistence.rb
+++ b/activerecord/lib/active_record/persistence.rb
@@ -479,15 +479,15 @@ module ActiveRecord
verify_readonly_attribute(key.to_s)
end
+ attributes.each do |k, v|
+ write_attribute_without_type_cast(k, v)
+ end
+
affected_rows = self.class._update_record(
attributes,
self.class.primary_key => id_in_database
)
- attributes.each do |k, v|
- write_attribute_without_type_cast(k, v)
- end
-
affected_rows == 1
end
diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb
index 0bfd46a522..6c2e256447 100644
--- a/activerecord/test/cases/attribute_methods_test.rb
+++ b/activerecord/test/cases/attribute_methods_test.rb
@@ -310,6 +310,12 @@ class AttributeMethodsTest < ActiveRecord::TestCase
assert_equal "New topic", topic.title
end
+ test "write_attribute raises ActiveModel::MissingAttributeError when the attribute does not exist" do
+ topic = Topic.first
+ assert_raises(ActiveModel::MissingAttributeError) { topic.update_columns(no_column_exists: "Hello!") }
+ assert_raises(ActiveModel::UnknownAttributeError) { topic.update(no_column_exists: "Hello!") }
+ end
+
test "read_attribute" do
topic = Topic.new
topic.title = "Don't change the topic"