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

Ticket #8117: calculate_grouped_association_with_foreign_key.patch

File calculate_grouped_association_with_foreign_key.patch, 1.7 kB (added by kamal, 1 year ago)

Applied jack's suggestion to the original patch with tests, tested against latest revision of activerecord r7037

  • activerecord/test/calculations_test.rb

    old new  
    159159      assert_equal 1, c.first.last 
    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} 
    165173    options_copy = options.dup 
  • activerecord/lib/active_record/calculations.rb

    old new  
    221221          group_attr      = options[:group].to_s 
    222222          association     = reflect_on_association(group_attr.to_sym) 
    223223          associated      = association && association.macro == :belongs_to # only count belongs_to associations 
    224           group_field     = (associated ? "#{options[:group]}_id" : options[:group]).to_s 
     224          group_field     = associated ? association.primary_key_name : group_attr 
    225225          group_alias     = column_alias_for(group_field) 
    226226          group_column    = column_for group_field 
    227227          sql             = construct_calculation_sql(operation, column_name, options.merge(:group_field => group_field, :group_alias => group_alias))