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) |
|---|
-
test/schema/schema.rb
old new 355 355 create_table 'warehouse-things', :force => true do |t| 356 356 t.integer :value 357 357 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 358 368 end -
test/models/pet.rb
old new 1 class Pet < ActiveRecord::Base 2 set_primary_key :pet_id 3 belongs_to :owner 4 end -
test/models/owner.rb
old new 1 class Owner < ActiveRecord::Base 2 set_primary_key :owner_id 3 has_many :pets 4 end -
test/fixtures/pets.yml
old new 1 parrot: 2 pet_id: 1 3 name: parrot 4 owner_id: 1 5 6 chew: 7 pet_id: 2 8 name: chew 9 owner_id: 2 10 11 mochi: 12 pet_id: 3 13 name: mochi 14 owner_id: 2 -
test/fixtures/owners.yml
old new 1 blackbeard: 2 owner_id: 1 3 name: blackbeard 4 5 ashley: 6 owner_id: 2 7 name: ashley -
test/cases/associations/eager_test.rb
old new 7 7 require 'models/company' 8 8 require 'models/person' 9 9 require 'models/reader' 10 require 'models/owner' 11 require 'models/pet' 10 12 11 13 class EagerAssociationTest < ActiveSupport::TestCase 12 14 fixtures :posts, :comments, :authors, :categories, :categories_posts, 13 :companies, :accounts, :tags, :taggings, :people, :readers 15 :companies, :accounts, :tags, :taggings, :people, :readers, 16 :owners, :pets 14 17 15 18 def test_loading_with_one_association 16 19 posts = Post.find(:all, :include => :comments) … … 73 76 assert_nil Post.find(posts(:authorless).id, :include => :author).author 74 77 end 75 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 82 end 83 76 84 def test_eager_association_loading_with_belongs_to 77 85 comments = Comment.find(:all, :include => :post) 78 86 assert_equal 10, comments.length -
lib/active_record/association_preload.rb
old new 180 180 klass = klass_name.constantize 181 181 182 182 table_name = klass.table_name 183 primary_key = klass.primary_key 183 184 conditions = "#{table_name}.#{primary_key} IN (?)" 184 185 conditions << append_conditions(options, preload_options) 185 186 associated_records = klass.find(:all, :conditions => [conditions, id_map.keys.uniq], … … 187 188 :select => options[:select], 188 189 :joins => options[:joins], 189 190 :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) 191 192 end 192 193 end 193 194