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

Changeset 8762

Show
Ignore:
Timestamp:
01/31/08 07:50:15 (4 months ago)
Author:
bitsweat
Message:

Fixed preloading belongs_to associations which reference a custom foreign key. References #9640.

Files:

Legend:

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

    r8757 r8762  
    55* belongs_to supports :dependent => :destroy and :delete.  #10592 [Jonathan Viney] 
    66 
    7 * Introduce preload query strategy for eager :includes.  #9640 [Frederick Cheung, Aleksey Kondratenko
     7* Introduce preload query strategy for eager :includes.  #9640 [Frederick Cheung, Aleksey Kondratenko, codafoo
    88 
    99* Support aggregations in finder conditions.  #10572 [Ryan Kinderman] 
  • trunk/activerecord/lib/active_record/association_preload.rb

    r8672 r8762  
    181181 
    182182          table_name = klass.table_name 
     183          primary_key = klass.primary_key 
    183184          conditions = "#{table_name}.#{primary_key} IN (?)" 
    184185          conditions << append_conditions(options, preload_options) 
     
    188189                                          :joins => options[:joins], 
    189190                                          :order => options[:order]) 
    190           set_association_single_records(id_map, reflection.name, associated_records, 'id'
     191          set_association_single_records(id_map, reflection.name, associated_records, primary_key
    191192        end 
    192193      end 
  • trunk/activerecord/test/cases/associations/eager_test.rb

    r8681 r8762  
    88require 'models/person' 
    99require 'models/reader' 
     10require 'models/owner' 
     11require 'models/pet' 
    1012 
    1113class EagerAssociationTest < ActiveRecord::TestCase 
    1214  fixtures :posts, :comments, :authors, :categories, :categories_posts, 
    13             :companies, :accounts, :tags, :taggings, :people, :readers 
     15            :companies, :accounts, :tags, :taggings, :people, :readers, 
     16            :owners, :pets 
    1417 
    1518  def test_loading_with_one_association 
     
    7275  def test_loading_with_no_associations 
    7376    assert_nil Post.find(posts(:authorless).id, :include => :author).author 
     77  end 
     78 
     79  def test_eager_association_loading_with_belongs_to_and_foreign_keys 
     80    pets = Pet.find(:all, :include => :owner) 
     81    assert_equal 3, pets.length 
    7482  end 
    7583 
  • trunk/activerecord/test/schema/schema.rb

    r8745 r8762  
    359359    t.integer :value 
    360360  end 
     361 
     362  create_table :owners, :primary_key => :owner_id ,:force => true do |t| 
     363    t.string :name 
     364  end 
     365 
     366  create_table :pets, :primary_key => :pet_id ,:force => true do |t| 
     367    t.string :name 
     368    t.integer :owner_id, :integer 
     369  end 
    361370end