Skip to content

Commit

Permalink
Add array id support to Model.update_counters. [#1254 state:resolved]…
Browse files Browse the repository at this point in the history
… [Carlos Júnior]
  • Loading branch information
lifo committed Jan 28, 2009
1 parent 2ae8d30 commit 7487196
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
19 changes: 17 additions & 2 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -927,7 +927,7 @@ def count_by_sql(sql)
#
# ==== Parameters
#
# * +id+ - The id of the object you wish to update a counter on.
# * +id+ - The id of the object you wish to update a counter on or an Array of ids.
# * +counters+ - An Array of Hashes containing the names of the fields
# to update as keys and the amount to update the field by as values.
#
Expand All @@ -941,12 +941,27 @@ def count_by_sql(sql)
# # SET comment_count = comment_count - 1,
# # action_count = action_count + 1
# # WHERE id = 5
#
# # For the Posts with id of 10 and 15, increment the comment_count by 1
# Post.update_counters [10, 15], :comment_count => 1
# # Executes the following SQL:
# # UPDATE posts
# # SET comment_count = comment_count + 1,
# # WHERE id IN (10, 15)
def update_counters(id, counters)
updates = counters.inject([]) { |list, (counter_name, increment)|
sign = increment < 0 ? "-" : "+"
list << "#{connection.quote_column_name(counter_name)} = COALESCE(#{connection.quote_column_name(counter_name)}, 0) #{sign} #{increment.abs}"
}.join(", ")
update_all(updates, "#{connection.quote_column_name(primary_key)} = #{quote_value(id)}")

if id.is_a?(Array)
ids_list = id.map {|i| quote_value(i)}.join(', ')
condition = "IN (#{ids_list})"
else
condition = "= #{quote_value(id)}"
end

update_all(updates, "#{connection.quote_column_name(primary_key)} #{condition}")
end

# Increment a number field by one, usually representing a count.
Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/base_test.rb
Expand Up @@ -639,6 +639,13 @@ def test_update_counter
category.reload
assert_not_nil category.categorizations_count
assert_equal 4, category.categorizations_count

category_2 = categories(:technology)
count_1, count_2 = (category.categorizations_count || 0), (category_2.categorizations_count || 0)
Category.update_counters([category.id, category_2.id], "categorizations_count" => 2)
category.reload; category_2.reload
assert_equal count_1 + 2, category.categorizations_count
assert_equal count_2 + 2, category_2.categorizations_count
end

def test_update_all
Expand Down

0 comments on commit 7487196

Please sign in to comment.