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

Ticket #9640: fix_belongs_to_with_foreign_key.patch

File fix_belongs_to_with_foreign_key.patch, 4.0 kB (added by codafoo, 4 months ago)

Fixed a bug with preloading belongs_to associations that doesn't use 'id' as the primary_key.

  • test/schema/schema.rb

    old new  
    355355  create_table 'warehouse-things', :force => true do |t| 
    356356    t.integer :value 
    357357  end 
     358 
     359  create_table :owners, :primary_key => :owner_id ,:force => true do |t| 
     360    t.string :name 
     361  end 
     362   
     363  create_table :pets, :primary_key => :pet_id ,:force => true do |t| 
     364    t.string :name 
     365    t.integer :owner_id, :integer 
     366  end 
     367   
    358368end 
  • test/models/pet.rb

    old new  
     1class Pet < ActiveRecord::Base 
     2  set_primary_key :pet_id 
     3  belongs_to :owner 
     4end 
  • test/models/owner.rb

    old new  
     1class Owner < ActiveRecord::Base 
     2  set_primary_key :owner_id 
     3  has_many :pets 
     4end 
  • test/fixtures/pets.yml

    old new  
     1parrot: 
     2    pet_id: 1 
     3    name: parrot 
     4    owner_id: 1 
     5 
     6chew: 
     7    pet_id: 2 
     8    name: chew 
     9    owner_id: 2 
     10     
     11mochi: 
     12    pet_id: 3 
     13    name: mochi 
     14    owner_id: 2 
  • test/fixtures/owners.yml

    old new  
     1blackbeard: 
     2    owner_id: 1 
     3    name: blackbeard 
     4 
     5ashley: 
     6    owner_id: 2 
     7    name: ashley 
  • test/cases/associations/eager_test.rb

    old new  
    77require 'models/company' 
    88require 'models/person' 
    99require 'models/reader' 
     10require 'models/owner' 
     11require 'models/pet' 
    1012 
    1113class EagerAssociationTest < ActiveSupport::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 
    1619    posts = Post.find(:all, :include => :comments) 
     
    7376    assert_nil Post.find(posts(:authorless).id, :include => :author).author 
    7477  end 
    7578 
     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 
     82  end 
     83 
    7684  def test_eager_association_loading_with_belongs_to 
    7785    comments = Comment.find(:all, :include => :post) 
    7886    assert_equal 10, comments.length 
  • lib/active_record/association_preload.rb

    old new  
    180180          klass = klass_name.constantize 
    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) 
    185186          associated_records = klass.find(:all, :conditions => [conditions, id_map.keys.uniq], 
     
    187188                                          :select => options[:select], 
    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 
    193194