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

Changeset 8779

Show
Ignore:
Timestamp:
02/02/08 04:56:11 (5 months ago)
Author:
bitsweat
Message:

Merge r8778 from trunk: fix calculations on associations with custom :foreign_key. References #8117.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/2-0-stable/activerecord/lib/active_record/calculations.rb

    r8381 r8779  
    214214          association     = reflect_on_association(group_attr.to_sym) 
    215215          associated      = association && association.macro == :belongs_to # only count belongs_to associations 
    216           group_field     = (associated ? "#{options[:group]}_id" : options[:group]).to_s 
     216          group_field     = associated ? association.primary_key_name : group_attr 
    217217          group_alias     = column_alias_for(group_field) 
    218218          group_column    = column_for group_field 
  • branches/2-0-stable/activerecord/test/calculations_test.rb

    r7192 r8779  
    160160    end 
    161161  end 
    162    
     162 
     163  def test_should_calculate_grouped_association_with_foreign_key_option 
     164    Account.belongs_to :another_firm, :class_name => 'Firm', :foreign_key => 'firm_id' 
     165    c = Account.count(:all, :group => :another_firm) 
     166    assert_equal 1, c[companies(:first_firm)] 
     167    assert_equal 2, c[companies(:rails_core)] 
     168    assert_equal 1, c[companies(:first_client)] 
     169  end 
     170 
    163171  def test_should_not_modify_options_when_using_includes 
    164172    options = {:conditions => 'companies.id > 1', :include => :firm}