mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
count method should not take options if it is operated on has_many association which has finder_sql or counter_sql
[#2395 state:resolved] Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
@@ -183,10 +183,13 @@ module ActiveRecord
|
||||
# descendant's +construct_sql+ method will have set :counter_sql automatically.
|
||||
# Otherwise, construct options and pass them with scope to the target class's +count+.
|
||||
def count(column_name = nil, options = {})
|
||||
if @reflection.options[:counter_sql]
|
||||
column_name, options = nil, column_name if column_name.is_a?(Hash)
|
||||
|
||||
if @reflection.options[:counter_sql] && !options.blank?
|
||||
raise ArgumentError, "If finder_sql/counter_sql is used then options cannot be passed"
|
||||
elsif @reflection.options[:counter_sql]
|
||||
@reflection.klass.count_by_sql(@counter_sql)
|
||||
else
|
||||
column_name, options = nil, column_name if column_name.is_a?(Hash)
|
||||
|
||||
if @reflection.options[:uniq]
|
||||
# This is needed because 'SELECT count(DISTINCT *)..' is not valid SQL.
|
||||
|
||||
@@ -11,6 +11,32 @@ require 'models/comment'
|
||||
require 'models/person'
|
||||
require 'models/reader'
|
||||
require 'models/tagging'
|
||||
require 'models/invoice'
|
||||
require 'models/line_item'
|
||||
|
||||
class HasManyAssociationsTestForCountWithFinderSql < ActiveRecord::TestCase
|
||||
class Invoice < ActiveRecord::Base
|
||||
has_many :custom_line_items, :class_name => 'LineItem', :finder_sql => "SELECT line_items.* from line_items"
|
||||
end
|
||||
def test_should_fail
|
||||
assert_raise(ArgumentError) do
|
||||
Invoice.create.custom_line_items.count(:conditions => {:amount => 0})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class HasManyAssociationsTestForCountWithCountSql < ActiveRecord::TestCase
|
||||
class Invoice < ActiveRecord::Base
|
||||
has_many :custom_line_items, :class_name => 'LineItem', :counter_sql => "SELECT COUNT(*) line_items.* from line_items"
|
||||
end
|
||||
def test_should_fail
|
||||
assert_raise(ArgumentError) do
|
||||
Invoice.create.custom_line_items.count(:conditions => {:amount => 0})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
class HasManyAssociationsTest < ActiveRecord::TestCase
|
||||
fixtures :accounts, :categories, :companies, :developers, :projects,
|
||||
|
||||
Reference in New Issue
Block a user