Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6919

Show
Ignore:
Timestamp:
06/01/07 03:26:51 (2 years ago)
Author:
bitsweat
Message:

Calculations support non-numeric foreign keys. Closes #8154.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r6909 r6919  
    11*SVN* 
     2 
     3* Calculations support non-numeric foreign keys.  #8154 [kamal] 
    24 
    35* with_scope is protected.  #8524 [Josh Peek] 
  • trunk/activerecord/lib/active_record/calculations.rb

    r6904 r6919  
    236236 
    237237          calculated_data.inject(ActiveSupport::OrderedHash.new) do |all, row| 
    238             key   = associated ? key_records[row[group_alias].to_i] : type_cast_calculated_value(row[group_alias], group_column) 
     238            key   = type_cast_calculated_value(row[group_alias], group_column) 
     239            key   = key_records[key] if associated 
    239240            value = row[aggregate_alias] 
    240241            all << [key, type_cast_calculated_value(value, column, operation)] 
  • trunk/activerecord/test/calculations_test.rb

    r6904 r6919  
    139139    assert_equal 2, c[companies(:rails_core)] 
    140140    assert_equal 1, c[companies(:first_client)] 
     141  end 
     142 
     143  uses_mocha 'group_by_non_numeric_foreign_key_association' do 
     144    def test_should_group_by_association_with_non_numeric_foreign_key 
     145      ActiveRecord::Base.connection.expects(:select_all).returns([{"count_all" => 1, "firm_id" => "ABC"}]) 
     146 
     147      firm = mock() 
     148      firm.expects(:id).returns("ABC") 
     149      firm.expects(:class).returns(Firm) 
     150      Company.expects(:find).with(["ABC"]).returns([firm]) 
     151 
     152      column = mock() 
     153      column.expects(:name).at_least_once.returns(:firm_id) 
     154      column.expects(:type_cast).with("ABC").returns("ABC") 
     155      Account.expects(:columns).at_least_once.returns([column]) 
     156 
     157      c = Account.count(:all, :group => :firm) 
     158      assert_equal Firm, c.first.first.class 
     159      assert_equal 1, c.first.last 
     160    end 
    141161  end 
    142162