Enable update_column(s) for the primary key attribute.
Didn't work before because it updated the model-in-memory first, so the DB query couldn't find the record.
This commit is contained in:
parent
5bbe245a51
commit
1849665f73
|
@ -1,5 +1,9 @@
|
||||||
## Rails 4.0.0 (unreleased) ##
|
## Rails 4.0.0 (unreleased) ##
|
||||||
|
|
||||||
|
* Fix bug where `update_columns` and `update_column` would not let you update the primary key column.
|
||||||
|
|
||||||
|
*Henrik Nyh*
|
||||||
|
|
||||||
* The `create_table` method raises an `ArgumentError` when the primary key column is redefined.
|
* The `create_table` method raises an `ArgumentError` when the primary key column is redefined.
|
||||||
Fix #6378
|
Fix #6378
|
||||||
|
|
||||||
|
|
|
@ -224,11 +224,13 @@ module ActiveRecord
|
||||||
verify_readonly_attribute(key.to_s)
|
verify_readonly_attribute(key.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
updated_count = self.class.where(self.class.primary_key => id).update_all(attributes)
|
||||||
|
|
||||||
attributes.each do |k,v|
|
attributes.each do |k,v|
|
||||||
raw_write_attribute(k,v)
|
raw_write_attribute(k,v)
|
||||||
end
|
end
|
||||||
|
|
||||||
self.class.where(self.class.primary_key => id).update_all(attributes) == 1
|
updated_count == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
# Initializes +attribute+ to zero if +nil+ and adds the value passed as +by+ (default is 1).
|
# Initializes +attribute+ to zero if +nil+ and adds the value passed as +by+ (default is 1).
|
||||||
|
|
|
@ -592,6 +592,19 @@ class PersistencesTest < ActiveRecord::TestCase
|
||||||
assert_equal 'super_title', t.title
|
assert_equal 'super_title', t.title
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_update_columns_changing_id
|
||||||
|
topic = Topic.find(1)
|
||||||
|
topic.update_columns(id: 123)
|
||||||
|
assert_equal 123, topic.id
|
||||||
|
topic.reload
|
||||||
|
assert_equal 123, topic.id
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_update_columns_returns_boolean
|
||||||
|
topic = Topic.find(1)
|
||||||
|
assert_equal true, topic.update_columns(title: "New title")
|
||||||
|
end
|
||||||
|
|
||||||
def test_update_attributes
|
def test_update_attributes
|
||||||
topic = Topic.find(1)
|
topic = Topic.find(1)
|
||||||
assert !topic.approved?
|
assert !topic.approved?
|
||||||
|
|
Loading…
Reference in New Issue