Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Unbreak update_column/update_columns for the primary key attribute. #8053

Merged
merged 1 commit into from

5 participants

@henrik

Didn't work before because it updated the model-in-memory first, so the DB query couldn't find the record.

activerecord/lib/active_record/persistence.rb
@@ -224,11 +224,11 @@ def update_columns(attributes)
verify_readonly_attribute(key.to_s)
end
+ self.class.where(self.class.primary_key => id).update_all(attributes) == 1
@josevalim Owner

This change is modifying the return value of update_column. We should probably add a test to ensure it returns a boolean.

@henrik
henrik added a note

Yeah, I just noticed this. Should I go ahead and fix? Any preference between amend/force push vs. multiple commits in a feature branch?

@steveklabnik Collaborator

Generally we prefer commits to be squashed, so amend/force push.

@henrik
henrik added a note

Alright, pushed an amended commit: henrik@cd0efa5

Just tested that it returns true. Good enough? Seems the return value is undocumented anyway, and I'm not sure what it'd take to make it return false. Multiple records with the same id or for the query to somehow update nothing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
activerecord/CHANGELOG.md
@@ -1,5 +1,9 @@
## Rails 4.0.0 (unreleased) ##
+* Fix bug where update\_attributes and update\_attribute would not let you update the primary key column.

Update attribute(s)?

@henrik
henrik added a note

I used "update_attribute(s)" for the commit message but figured I should avoid the possible confusion with method invocation, in the CHANGELOG. I'd be fine with either though!

I meant to say that you're fixing a "bug" in update_column/update_columns methods, not update_attribute(s) as the changelog says.

@henrik
henrik added a note

Oh, sorry! Fixing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
activerecord/lib/active_record/persistence.rb
((6 lines not shown))
attributes.each do |k,v|
raw_write_attribute(k,v)
end
- self.class.where(self.class.primary_key => id).update_all(attributes) == 1
+ result_of_update == 1
end

How about update_count?

@henrik
henrik added a note

Good idea. Changed it to updated_count.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@carlosantoniodasilva

@henrik I think it's "the query updating nothing", in any case, it's just better to not change the result value to avoid possible breakage (and it'd be weird to return the attributes enumerable anyways). I made small comments, but overall it looks good, thanks!

@carlosantoniodasilva

@henrik thanks. Github says it cannot be merged anymore, can you rebase it from master please? It's probably the changelog.

@carlosantoniodasilva

And since you're touching the changelog, how about using backticks like this instead of \ escaping: "update_columns and update_column"

@rafaelfranca

Do you mean this? `update_column` and `update_column`

@henrik

Good idea - used backticks, rebased against master.

@henrik

Oops, a merge commit snuck in there. Fixing.

@henrik henrik 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.
1849665
@henrik

And there we go.

@carlosantoniodasilva carlosantoniodasilva merged commit c6f47c1 into rails:master
@carlosantoniodasilva

@henrik awesome, thanks.

@henrik

Wow, that was faster and less ceremony than expected. Thanks a lot, guys!

@rafaelfranca

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 28, 2012
  1. @henrik

    Enable update_column(s) for the primary key attribute.

    henrik authored
    Didn't work before because it updated the model-in-memory first, so the DB query couldn't find the record.
This page is out of date. Refresh to see the latest.
View
4 activerecord/CHANGELOG.md
@@ -1,5 +1,9 @@
## 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.
Fix #6378
View
4 activerecord/lib/active_record/persistence.rb
@@ -224,11 +224,13 @@ def update_columns(attributes)
verify_readonly_attribute(key.to_s)
end
+ updated_count = self.class.where(self.class.primary_key => id).update_all(attributes)
+
attributes.each do |k,v|
raw_write_attribute(k,v)
end
- self.class.where(self.class.primary_key => id).update_all(attributes) == 1
+ updated_count == 1
end
# Initializes +attribute+ to zero if +nil+ and adds the value passed as +by+ (default is 1).
View
13 activerecord/test/cases/persistence_test.rb
@@ -592,6 +592,19 @@ def test_update_columns_with_one_changed_and_one_updated
assert_equal 'super_title', t.title
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
topic = Topic.find(1)
assert !topic.approved?
Something went wrong with that request. Please try again.