diff --git a/lib/squeel/adapters/active_record/4.0/relation_extensions.rb b/lib/squeel/adapters/active_record/4.0/relation_extensions.rb index 85329b8..32ec58c 100644 --- a/lib/squeel/adapters/active_record/4.0/relation_extensions.rb +++ b/lib/squeel/adapters/active_record/4.0/relation_extensions.rb @@ -120,40 +120,6 @@ def dehashified_order_values end } end - - # So, building a select for a count query in Active Record is - # pretty heavily dependent on select_values containing strings. - # I'd initially expected that I could just hack together a fix - # to select_for_count and everything would fall in line, but - # unfortunately, pretty much everything from that point on - # in ActiveRecord::Calculations#perform_calculation expects - # the column to be a string, or at worst, a symbol. - # - # In the long term, I would like to refactor the code in - # Rails core, but for now, I'm going to settle for this hack - # that tries really hard to coerce things to a string. - def select_for_count - visited_values = select_visit(select_values.uniq) - if visited_values.any? - string_values = visited_values.map { |value| - case value - when String - value - when Symbol - value.to_s - when Arel::Attributes::Attribute - join_name = value.relation.table_alias || value.relation.name - "#{connection.quote_table_name join_name}.#{connection.quote_column_name value.name}" - else - value.respond_to?(:to_sql) ? value.to_sql : value - end - } - string_values.join(', ') - else - :all - end - end - end end end diff --git a/lib/squeel/adapters/active_record/relation_extensions.rb b/lib/squeel/adapters/active_record/relation_extensions.rb index 14ac4a3..308af31 100644 --- a/lib/squeel/adapters/active_record/relation_extensions.rb +++ b/lib/squeel/adapters/active_record/relation_extensions.rb @@ -99,6 +99,8 @@ def select_for_count end str_select if str_select && str_select !~ /[,*]/ + else + :all end end diff --git a/spec/squeel/adapters/active_record/relation_extensions_spec.rb b/spec/squeel/adapters/active_record/relation_extensions_spec.rb index c874b18..c2a55a9 100644 --- a/spec/squeel/adapters/active_record/relation_extensions_spec.rb +++ b/spec/squeel/adapters/active_record/relation_extensions_spec.rb @@ -389,6 +389,10 @@ module ActiveRecord expect { people.first.name }.to raise_error ActiveModel::MissingAttributeError end + it 'works with multiple fields in select' do + Article.select("title, body").count.should eq 51 + end + it 'allows a function in the select values via Symbol#func' do relation = Person.select(:max.func(:id).as('max_id')) relation.first.max_id.should eq 332