Skip to content
Browse files

Merge pull request #7133 from roshats/fix_update_all_with_blank_argument

Change Relation#update_all with blank argument to raise an ArgumentError 
instead of trying an update with empty fields.
  • Loading branch information...
2 parents 0d0d462 + f280964 commit cf232e37adc8cdf570a04415ba4c51a73dd06463 @carlosantoniodasilva carlosantoniodasilva committed
View
4 activerecord/CHANGELOG.md
@@ -586,6 +586,10 @@
* PostgreSQL hstore types are automatically deserialized from the database.
+* Raise `ArgumentError` if list of attributes to change is empty in `update_all`.
+
+ *Roman Shatsov*
+
## Rails 3.2.8 (Aug 9, 2012) ##
View
4 activerecord/lib/active_record/relation.rb
@@ -258,6 +258,8 @@ def scoping
# # Update all books that match conditions, but limit it to 5 ordered by date
# Book.where('title LIKE ?', '%Rails%').order(:created_at).limit(5).update_all(:author => 'David')
def update_all(updates)
+ raise ArgumentError, "Empty list of attributes to change" if updates.blank?
+
stmt = Arel::UpdateManager.new(arel.engine)
stmt.set Arel.sql(@klass.send(:sanitize_sql_for_assignment, updates))
@@ -466,7 +468,7 @@ def reset
# Returns sql statement for the relation.
#
# Users.where(name: 'Oscar').to_sql
- # # => SELECT "users".* FROM "users" WHERE "users"."name" = 'Oscar'
+ # # => SELECT "users".* FROM "users" WHERE "users"."name" = 'Oscar'
def to_sql
@to_sql ||= klass.connection.to_sql(arel, bind_values.dup)
end
View
4 activerecord/test/cases/relations_test.rb
@@ -1122,6 +1122,10 @@ def test_ordering_with_extra_spaces
assert_equal authors(:david), Author.order('id DESC , name DESC').last
end
+ def test_update_all_with_blank_argument
+ assert_raises(ArgumentError) { Comment.update_all({}) }
+ end
+
def test_update_all_with_joins
comments = Comment.joins(:post).where('posts.id' => posts(:welcome).id)
count = comments.count

0 comments on commit cf232e3

Please sign in to comment.
Something went wrong with that request. Please try again.