diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-11-15 13:42:35 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2012-11-15 13:42:50 -0800 |
commit | 0803d7a93b4a67340719eedddcc1746414fc2405 (patch) | |
tree | 3f14182040d437c5ed5470910af604284496671e | |
parent | ca71bb8c99cb78fde6794cd2134b4b6147aa4426 (diff) | |
download | rails-0803d7a93b4a67340719eedddcc1746414fc2405.tar.gz rails-0803d7a93b4a67340719eedddcc1746414fc2405.tar.bz2 rails-0803d7a93b4a67340719eedddcc1746414fc2405.zip |
arel columns can be used for grouping so that "weird" column names are usable
-rw-r--r-- | activerecord/lib/active_record/relation/calculations.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 6 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 1 |
3 files changed, 11 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 8054294954..f02ed62275 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -335,6 +335,10 @@ module ActiveRecord # column_alias_for("count(*)") # => "count_all" # column_alias_for("count", "id") # => "count_id" def column_alias_for(keys) + if keys.respond_to? :name + keys = "#{keys.relation.name}.#{keys.name}" + end + table_name = keys.to_s.downcase table_name.gsub!(/\*/, 'all') table_name.gsub!(/\W+/, ' ') diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index c02d8f7760..8644f2f496 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -616,6 +616,12 @@ class BasicsTest < ActiveRecord::TestCase assert_equal 'value2', weird.read_attribute('a$b') end + def test_group_weirds_by_from + Weird.create('a$b' => 'value', :from => 'aaron') + count = Weird.group(Weird.arel_table[:from]).count + assert_equal 1, count['aaron'] + end + def test_attributes_on_dummy_time # Oracle, and Sybase do not have a TIME datatype. return true if current_adapter?(:OracleAdapter, :SybaseAdapter) diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index eec06754a5..35778d008a 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -770,6 +770,7 @@ ActiveRecord::Schema.define do end create_table :weirds, :force => true do |t| t.string 'a$b' + t.string 'from' end except 'SQLite' do |