Skip to content

Commit

Permalink
update_counters should update nil values.
Browse files Browse the repository at this point in the history
This allows counter columns with default null instead of requiring default 0.

[#493 state:resolved]
  • Loading branch information
miloops authored and jeremy committed Jul 15, 2008
1 parent 04f7ac5 commit f253e98
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ def count_by_sql(sql)
def update_counters(id, counters)
updates = counters.inject([]) { |list, (counter_name, increment)|
sign = increment < 0 ? "-" : "+"
list << "#{connection.quote_column_name(counter_name)} = #{connection.quote_column_name(counter_name)} #{sign} #{increment.abs}"
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)}")
end
Expand Down
21 changes: 19 additions & 2 deletions activerecord/test/cases/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require 'rexml/document'

class Category < ActiveRecord::Base; end
class Categorization < ActiveRecord::Base; end
class Smarts < ActiveRecord::Base; end
class CreditCard < ActiveRecord::Base
class PinNumber < ActiveRecord::Base
Expand Down Expand Up @@ -75,7 +76,7 @@ class TopicWithProtectedContentAndAccessibleAuthorName < ActiveRecord::Base
end

class BasicsTest < ActiveRecord::TestCase
fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors
fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors, :categorizations

def test_table_exists
assert !NonExistentTable.table_exists?
Expand Down Expand Up @@ -130,7 +131,7 @@ def test_array_content

def test_read_attributes_before_type_cast
category = Category.new({:name=>"Test categoty", :type => nil})
category_attrs = {"name"=>"Test categoty", "type" => nil}
category_attrs = {"name"=>"Test categoty", "type" => nil, "categorizations_count" => nil}
assert_equal category_attrs , category.attributes_before_type_cast
end

Expand Down Expand Up @@ -614,6 +615,22 @@ def test_decrement_counter
assert_equal -2, Topic.find(2).replies_count
end

def test_update_counter
category = Category.first
assert_nil category.categorizations_count
assert_equal 2, category.categorizations.count

Category.update_counters(category.id, "categorizations_count" => category.categorizations.count)
category.reload
assert_not_nil category.categorizations_count
assert_equal 2, category.categorizations_count

Category.update_counters(category.id, "categorizations_count" => category.categorizations.count)
category.reload
assert_not_nil category.categorizations_count
assert_equal 4, category.categorizations_count
end

def test_update_all
assert_equal Topic.count, Topic.update_all("content = 'bulk updated!'")
assert_equal "bulk updated!", Topic.find(1).content
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/schema/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def create_table(*args, &block)
create_table :categories, :force => true do |t|
t.string :name, :null => false
t.string :type
t.integer :categorizations_count
end

create_table :categories_posts, :force => true, :id => false do |t|
Expand Down

0 comments on commit f253e98

Please sign in to comment.