This project is archived and is in readonly mode.

#994 ✓resolved
Edgars Beigarts

ActiveRecord calculations does not work with :distinct

Reported by Edgars Beigarts | September 8th, 2008 @ 01:58 PM | in 2.x


>> User.count(:distinct => false)
  SQL (0.465150)   SELECT count(*) AS count_all FROM users
=> 12
>> User.count(:distinct => true)
  SQL (0.000000)   OCIError: ORA-00911: invalid character: SELECT COUNT(*) AS count_all FROM (SELECT DISTINCT * FROM users ) AS #{aggregate_alias}_subquery
ActiveRecord::StatementInvalid: OCIError: ORA-00911: invalid character: SELECT COUNT(*) AS count_all FROM (SELECT DISTINCT * FROM users ) AS #{aggregate_alias}_subquery
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.1/lib/active_record/connection_adapters/abstract_adapter.rb:147:in `log'
	from /Users/edgarsbeigarts/code/izp/trunk/rails/vendor/gems/activerecord-oracle_enhanced-adapter-1.1.5/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb:345:in `execute'
	from /Users/edgarsbeigarts/code/izp/trunk/rails/vendor/gems/activerecord-oracle_enhanced-adapter-1.1.5/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb:666:in `select'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.1/lib/active_record/connection_adapters/abstract/query_cache.rb:61:in `select_all'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:13:in `select_one'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:19:in `select_value'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.1/lib/active_record/calculations.rb:225:in `execute_simple_calculation'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.1/lib/active_record/calculations.rb:126:in `calculate'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.1/lib/active_record/calculations.rb:122:in `catch'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.1/lib/active_record/calculations.rb:122:in `calculate'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.1/lib/active_record/calculations.rb:48:in `count'
	from (irb):16

Worked fine in 2.1.0 Does not work in 2.1.1

This line is wrong: http://github.com/rails/rails/tr... http://github.com/rails/rails/co...

  1. single quotes ?
  2. in Oracle and probably some other databases keyword AS in FROM is not valid

Quick monkey patch:


module ActiveRecord::Calculations::ClassMethods
  def construct_calculation_sql_with_proper_distinct(operation, column_name, options)
    bad_sql = construct_calculation_sql_without_proper_distinct(operation, column_name, options)
    bad_sql.gsub 'AS #{aggregate_alias}_subquery', ''
  end
  alias_method_chain :construct_calculation_sql, :proper_distinct
end

>> User.count(:distinct => true)
  SQL (0.010703)   SELECT COUNT(*) AS count_all FROM (SELECT DISTINCT * FROM users )
=> 12
>> User.count(:distinct => false)
  SQL (0.009651)   SELECT count(*) AS count_all FROM users
=> 12

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages