Added counter_sql option for has_many associations [bitsweat]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@68 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2004-12-07 12:25:01 +00:00
parent 465e0c0c26
commit ea759cb761
6 changed files with 69 additions and 43 deletions

View File

@ -1,5 +1,10 @@
*CVS*
* Added counter_sql option for has_many associations [bitsweat]. Documentation:
<tt>:counter_sql</tt> - specify a complete SQL statement to fetch the size of the association. If +:finder_sql+ is
specified but +:counter_sql+, +:counter_sql+ will be generated by replacing SELECT ... FROM with SELECT COUNT(*) FROM.
* Fixed that methods wrapped in callbacks still return their original result #260 [bitsweat]
* Fixed the Inflector to handle the movie/movies pair correctly #261 [Scott Baron]

View File

@ -166,6 +166,8 @@ module ActiveRecord
# May not be set if :dependent is also set.
# * <tt>:finder_sql</tt> - specify a complete SQL statement to fetch the association. This is a good way to go for complex
# associations that depends on multiple tables. Note: When this option is used, +find_in_collection+ is _not_ added.
# * <tt>:counter_sql</tt> - specify a complete SQL statement to fetch the size of the association. If +:finder_sql+ is
# specified but +:counter_sql+, +:counter_sql+ will be generated by replacing SELECT ... FROM with SELECT COUNT(*) FROM.
#
# Option examples:
# has_many :comments, :order => "posted_on"
@ -177,7 +179,7 @@ module ActiveRecord
# 'WHERE ps.post_id = #{id} AND ps.person_id = p.id ' +
# 'ORDER BY p.first_name'
def has_many(association_id, options = {})
validate_options([ :foreign_key, :class_name, :exclusively_dependent, :dependent, :conditions, :order, :finder_sql ], options.keys)
validate_options([ :foreign_key, :class_name, :exclusively_dependent, :dependent, :conditions, :order, :finder_sql, :counter_sql ], options.keys)
association_name, association_class_name, association_class_primary_key_name =
associate_identification(association_id, options[:class_name], options[:foreign_key])

View File

@ -7,9 +7,15 @@ module ActiveRecord
if options[:finder_sql]
@finder_sql = interpolate_sql(options[:finder_sql])
@counter_sql = @finder_sql.gsub(/SELECT (.*) FROM/i, "SELECT COUNT(*) FROM")
else
@finder_sql = "#{@association_class_primary_key_name} = '#{@owner.id}' #{@conditions ? " AND " + interpolate_sql(@conditions) : ""}"
end
if options[:counter_sql]
@counter_sql = interpolate_sql(options[:counter_sql])
elsif options[:finder_sql]
@counter_sql = options[:counter_sql] = @finder_sql.gsub(/SELECT (.*) FROM/i, "SELECT COUNT(*) FROM")
else
@counter_sql = "#{@association_class_primary_key_name} = '#{@owner.id}'#{@conditions ? " AND " + interpolate_sql(@conditions) : ""}"
end
end
@ -74,7 +80,7 @@ module ActiveRecord
def count_records
if has_cached_counter?
@owner.send(:read_attribute, cached_counter_attribute_name)
elsif @options[:finder_sql]
elsif @options[:counter_sql]
@association_class.count_by_sql(@counter_sql)
else
@association_class.count(@counter_sql)

View File

@ -178,6 +178,11 @@ class HasManyAssociationsTest < Test::Unit::TestCase
assert_equal 1, Firm.find_first.clients_using_sql.size
end
def test_counting_using_sql
assert_equal 1, Firm.find_first.clients_using_counter_sql.size
assert_equal 0, Firm.find_first.clients_using_zero_counter_sql.size
end
def test_find_all
assert_equal 2, Firm.find_first.clients.find_all("type = 'Client'").length
assert_equal 1, Firm.find_first.clients.find_all("name = 'Summit'").length

View File

@ -49,6 +49,10 @@ class DeprecatedAssociationsTest < Test::Unit::TestCase
assert_equal 1, Firm.find_first.clients_using_sql_count
end
def test_has_many_counter_sql
assert_equal 1, Firm.find_first.clients_using_counter_sql_count
end
def test_has_many_queries
assert Firm.find_first.has_clients?
firm = Firm.find_first
@ -106,7 +110,6 @@ class DeprecatedAssociationsTest < Test::Unit::TestCase
assert Company.find(3).has_firm_with_condition?, "Microsoft should have a firm"
end
def test_belongs_to_equality
assert Company.find(3).firm?(Company.find(1)), "Microsoft should have 37signals as firm"
assert_raises(RuntimeError) { !Company.find(3).firm?(Company.find(3)) } # "Summit shouldn't have itself as firm"
@ -280,7 +283,7 @@ class DeprecatedAssociationsTest < Test::Unit::TestCase
def test_natural_assignment_of_has_many
apple = Firm.create("name" => "Apple")
natural = Client.new("name" => "Natural Company")
natural = Client.create("name" => "Natural Company")
apple.clients << natural
assert_equal apple.id, natural.firm_id
assert_equal Client.find(natural.id), Firm.find(apple.id).clients.find { |c| c.id == natural.id }
@ -288,14 +291,13 @@ class DeprecatedAssociationsTest < Test::Unit::TestCase
assert_nil Firm.find(apple.id).clients.find { |c| c.id == natural.id }
end
def test_natural_adding_of_has_and_belongs_to_many
rails = Project.create("name" => "Rails")
ap = Project.create("name" => "Action Pack")
john = Developer.create("name" => "John")
mike = Developer.create("name" => "Mike")
rails.developers << john
rails.developers << mike
rails.developers << john
rails.developers << mike
assert_equal Developer.find(john.id), Project.find(rails.id).developers.find { |d| d.id == john.id }
assert_equal Developer.find(mike.id), Project.find(rails.id).developers.find { |d| d.id == mike.id }
@ -307,7 +309,7 @@ class DeprecatedAssociationsTest < Test::Unit::TestCase
ap.developers.delete john
assert_nil Project.find(ap.id).developers.find { |d| d.id == john.id }
assert_nil Developer.find(john.id).projects.find { |p| p.id == ap.id }
assert_nil Developer.find(john.id).projects.find { |p| p.id == ap.id }
end
def test_storing_in_pstore

View File

@ -9,6 +9,12 @@ class Firm < Company
has_many :clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id"
has_many :clients_like_ms, :conditions => "name = 'Microsoft'", :class_name => "Client", :order => "id"
has_many :clients_using_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}'
has_many :clients_using_counter_sql, :class_name => "Client",
:finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}',
:counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = #{id}'
has_many :clients_using_zero_counter_sql, :class_name => "Client",
:finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}',
:counter_sql => 'SELECT 0 FROM companies WHERE client_of = #{id}'
has_one :account, :dependent => true
end