Permalink
Browse files

Merge pull request #9707 from route/updates_another_record

When strong_parameters had been included, this case of update became possible
  • Loading branch information...
jonleighton committed Mar 22, 2013
2 parents 7748d64 + 89828a4 commit a1d0b8c67b514d2d339d8687f64de7a84b4700e5
@@ -1,4 +1,11 @@
## Rails 4.0.0 (unreleased) ##
+* After extraction of mass-assignment attributes (which protects [id, type]
+ by default) we can pass id to update_attributes and it will update
+ another record because id will be used in where statement. We never have
+ to change id in where statement because we try to set/replace fields for
+ already loaded record but we have to try to set new id for that record.
+
+ *Dmitry Vorotilin*
* Models with multiple counter cache associations now update correctly on destroy.
See #7706.
@@ -348,7 +348,7 @@ def arel_attributes_with_values(attribute_names)
# Filters the primary keys and readonly attributes from the attribute names.
def attributes_for_update(attribute_names)
attribute_names.select do |name|
- column_for_attribute(name) && !pk_attribute?(name) && !readonly_attribute?(name)
+ column_for_attribute(name) && !readonly_attribute?(name)
end
end
@@ -443,7 +443,7 @@ def update_record(attribute_names = @attributes.keys)
real_column = db_columns_with_values[i].first
bind_attrs[column] = klass.connection.substitute_at(real_column, i)
end
- stmt = klass.unscoped.where(klass.arel_table[klass.primary_key].eq(id)).arel.compile_update(bind_attrs)
+ stmt = klass.unscoped.where(klass.arel_table[klass.primary_key].eq(id_was)).arel.compile_update(bind_attrs)
klass.connection.update stmt, 'SQL', db_columns_with_values
end
end
@@ -661,6 +661,15 @@ def test_update_attributes
topic.reload
assert !topic.approved?
assert_equal "The First Topic", topic.title
+
+ assert_raise(ActiveRecord::RecordNotUnique, ActiveRecord::StatementInvalid) do
+ topic.update_attributes(id: 3, title: "Hm is it possible?")
+ end
+ assert_not_equal "Hm is it possible?", Topic.find(3).title
+
+ topic.update_attributes(id: 1234)
+ assert_nothing_raised { topic.reload }
+ assert_equal topic.title, Topic.find(1234).title
end
def test_update!

0 comments on commit a1d0b8c

Please sign in to comment.