mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make size for has_many :through use counter cache if it exists. Closes #9734 [xaviershay]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7692 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
e5a60fb2bf
commit
30a652ad41
3 changed files with 22 additions and 1 deletions
|
@ -1,5 +1,7 @@
|
||||||
*2.0.0 [Preview Release]* (September 29th, 2007) [Includes duplicates of changes from 1.14.2 - 1.15.3]
|
*2.0.0 [Preview Release]* (September 29th, 2007) [Includes duplicates of changes from 1.14.2 - 1.15.3]
|
||||||
|
|
||||||
|
* Make size for has_many :through use counter cache if it exists. Closes #9734 [xaviershay]
|
||||||
|
|
||||||
* Remove DB2 adapter since IBM chooses to maintain their own adapter instead. [Jeremy Kemper]
|
* Remove DB2 adapter since IBM chooses to maintain their own adapter instead. [Jeremy Kemper]
|
||||||
|
|
||||||
* Extract Oracle, SQLServer, and Sybase adapters into gems. [Jeremy Kemper]
|
* Extract Oracle, SQLServer, and Sybase adapters into gems. [Jeremy Kemper]
|
||||||
|
|
|
@ -100,7 +100,9 @@ module ActiveRecord
|
||||||
# calling collection.size if it has. If it's more likely than not that the collection does have a size larger than zero
|
# calling collection.size if it has. If it's more likely than not that the collection does have a size larger than zero
|
||||||
# and you need to fetch that collection afterwards, it'll take one less SELECT query if you use length.
|
# and you need to fetch that collection afterwards, it'll take one less SELECT query if you use length.
|
||||||
def size
|
def size
|
||||||
loaded? ? @target.size : count
|
return @owner.send(:read_attribute, cached_counter_attribute_name) if has_cached_counter?
|
||||||
|
return @target.size if loaded?
|
||||||
|
return count
|
||||||
end
|
end
|
||||||
|
|
||||||
# Calculate sum using SQL, not Enumerable
|
# Calculate sum using SQL, not Enumerable
|
||||||
|
@ -258,6 +260,14 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
alias_method :sql_conditions, :conditions
|
alias_method :sql_conditions, :conditions
|
||||||
|
|
||||||
|
def has_cached_counter?
|
||||||
|
@owner.attribute_present?(cached_counter_attribute_name)
|
||||||
|
end
|
||||||
|
|
||||||
|
def cached_counter_attribute_name
|
||||||
|
"#{@reflection.name}_count"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -457,6 +457,15 @@ class AssociationsJoinModelTest < Test::Unit::TestCase
|
||||||
assert !author.comments.loaded?
|
assert !author.comments.loaded?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
uses_mocha('has_many_through_collection_size_uses_counter_cache_if_it_exists') do
|
||||||
|
def test_has_many_through_collection_size_uses_counter_cache_if_it_exists
|
||||||
|
author = authors(:david)
|
||||||
|
author.stubs(:read_attribute).with('comments_count').returns(100)
|
||||||
|
assert_equal 100, author.comments.size
|
||||||
|
assert !author.comments.loaded?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_adding_junk_to_has_many_through_should_raise_type_mismatch
|
def test_adding_junk_to_has_many_through_should_raise_type_mismatch
|
||||||
assert_raise(ActiveRecord::AssociationTypeMismatch) { posts(:thinking).tags << "Uhh what now?" }
|
assert_raise(ActiveRecord::AssociationTypeMismatch) { posts(:thinking).tags << "Uhh what now?" }
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue