This project is archived and is in readonly mode.
[PATCH] ActiveRecord::Calculations#count doesn't allow table names in column_name argument
-
Neeraj Singh
- Importance changed from to Low
Can you post your Dish model with scope information? Also post schema info.
-
oleg dashevskii
Well, let's simplify the example:
User.count('orders.id', :joins => :orders, :group => 'users.id')Schema: User has many orders.
The
count()call without:groupworks:User.count('orders.id', :joins => :orders) -
Santiago Pastorino
- Milestone cleared.
- State changed from new to open
- Assigned user set to Aaron Patterson
-
oleg dashevskii
I made up a pair of tests which prove the bug.
-
Neeraj Singh
This would require changes in Arel because currently arel has following code.
def attribute(attribute) "#{quote_table_name(name_for(attribute.original_relation))}.#{quote_column_name(attribute.name)}" + (attribute.alias ? " AS #{quote(attribute.alias.to_s)}" : "") endThat needs to be changed to something like this
def attribute(attribute, prefix = true) table_name = "#{quote_table_name(name_for(attribute.original_relation))}" column_name = "#{quote_column_name(attribute.name)}" qualified_name = prefix ? "#{table_name}.#{column_name}" : column_name qualified_name + (attribute.alias ? " AS #{quote(attribute.alias.to_s)}" : "") endMethod execute_grouped_calculation in calculations.rb is executing
Arel::Attribute.new(r, column_name).send(operation).as(aggregate_alias).to_sqlThat needs to be changed to pass additional parameter to to_sql to tell it not to add prefix. Something like
Arel::Attribute.new(r, column_name).send(operation).as(aggregate_alias).to_sql(:table_name => false) -
oleg dashevskii
Since simple count (not grouped) works, I looked into its implementation:
def execute_simple_calculation(operation, column_name, distinct) #:nodoc: column = if @klass.column_names.include?(column_name.to_s) Arel::Attribute.new(@klass.unscoped, column_name) else Arel::SqlLiteral.new(column_name == :all ? "*" : column_name.to_s) end # Postgresql doesn't like ORDER BY when there are no GROUP BY relation = except(:order).select(operation == 'count' ? column.count(distinct) : column.send(operation)) type_cast_calculated_value(@klass.connection.select_value(relation.to_sql), column_for(column_name), operation) endCould this
Arel::SqlLiteralthing help here? I tried to copy this if-statement toexecute_grouped_calculation, but got very strange error messages somewhere from Arel. -
oleg dashevskii
- Title changed from ActiveRecord::Calculations#count doesn't allow table names in column_name argument to [PATCH] ActiveRecord::Calculations#count doesn't allow table names in column_name argument
- Tag changed from 3.0.0.rc2, :select, calculations to activerecord 3.0, 3.0.0.rc2, :select, calculations, patch
I fixed the bug with a rather brute-force style. This
@klass.send(:relation)thing apparently is a mess.Patch is attached.
-
Neeraj Singh
Nice work Oleg. Your patch fixes the problem.
For some reason when I tried to apply your patch it said email is invalid.
-
oleg dashevskii
Neeraj, it works for me (
git am < file). -
Repository
- State changed from open to resolved
(from [fc1bd2bba49a8c3f424b9d6f4c13f430f5db3d5c]) [#5441 [PATCH] ActiveRecord::Calculations#count doesn't allow table names in column_... state:resolved] refactoring code to determine aggregate column http://github.com/rails/rails/commit/fc1bd2bba49a8c3f424b9d6f4c13f4...
-
Repository
(from [a8a62f87f6777b1bad8f38cc4b0239b74a4f8a7a]) [#5441 [PATCH] ActiveRecord::Calculations#count doesn't allow table names in column_... state:resolved] refactoring code to determine aggregate column http://github.com/rails/rails/commit/a8a62f87f6777b1bad8f38cc4b0239...
