Skip to content

Commit

Permalink
raise ArgumentError if list of attributes to change is empty in updat…
Browse files Browse the repository at this point in the history
…e_all
  • Loading branch information
roshats committed Aug 14, 2012
1 parent 580fa0c commit f280964
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Expand Up @@ -529,6 +529,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.5 (Jun 1, 2012) ##

Expand Down
4 changes: 3 additions & 1 deletion activerecord/lib/active_record/relation.rb
Expand Up @@ -296,6 +296,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))
Expand Down Expand Up @@ -483,7 +485,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
Expand Down
4 changes: 4 additions & 0 deletions activerecord/test/cases/relations_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit f280964

Please sign in to comment.