diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2008-01-31 07:50:15 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-01-31 07:50:15 +0000 |
commit | be827f9348a703b3048cb7728d0de6d019279bd9 (patch) | |
tree | 951f051662b7c26754fa31ea05dce255523014ee /activerecord | |
parent | e59978aa867ef07e16ad64f73f1ed5cafa98d0ea (diff) | |
download | rails-be827f9348a703b3048cb7728d0de6d019279bd9.tar.gz rails-be827f9348a703b3048cb7728d0de6d019279bd9.tar.bz2 rails-be827f9348a703b3048cb7728d0de6d019279bd9.zip |
Fixed preloading belongs_to associations which reference a custom foreign key. References #9640.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8762 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/association_preload.rb | 3 | ||||
-rw-r--r-- | activerecord/test/cases/associations/eager_test.rb | 10 | ||||
-rw-r--r-- | activerecord/test/fixtures/owners.yml | 7 | ||||
-rw-r--r-- | activerecord/test/fixtures/pets.yml | 14 | ||||
-rw-r--r-- | activerecord/test/models/owner.rb | 4 | ||||
-rw-r--r-- | activerecord/test/models/pet.rb | 4 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 9 |
8 files changed, 50 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 5b232f93a2..c10bb9f416 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -4,7 +4,7 @@ * belongs_to supports :dependent => :destroy and :delete. #10592 [Jonathan Viney] -* Introduce preload query strategy for eager :includes. #9640 [Frederick Cheung, Aleksey Kondratenko] +* Introduce preload query strategy for eager :includes. #9640 [Frederick Cheung, Aleksey Kondratenko, codafoo] * Support aggregations in finder conditions. #10572 [Ryan Kinderman] diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index 6fa49dfbe8..87b087bfea 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -180,6 +180,7 @@ module ActiveRecord klass = klass_name.constantize table_name = klass.table_name + primary_key = klass.primary_key conditions = "#{table_name}.#{primary_key} IN (?)" conditions << append_conditions(options, preload_options) associated_records = klass.find(:all, :conditions => [conditions, id_map.keys.uniq], @@ -187,7 +188,7 @@ module ActiveRecord :select => options[:select], :joins => options[:joins], :order => options[:order]) - set_association_single_records(id_map, reflection.name, associated_records, 'id') + set_association_single_records(id_map, reflection.name, associated_records, primary_key) end end diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 9c258b574f..b606092d7e 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -7,10 +7,13 @@ require 'models/category' require 'models/company' require 'models/person' require 'models/reader' +require 'models/owner' +require 'models/pet' class EagerAssociationTest < ActiveRecord::TestCase fixtures :posts, :comments, :authors, :categories, :categories_posts, - :companies, :accounts, :tags, :taggings, :people, :readers + :companies, :accounts, :tags, :taggings, :people, :readers, + :owners, :pets def test_loading_with_one_association posts = Post.find(:all, :include => :comments) @@ -73,6 +76,11 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_nil Post.find(posts(:authorless).id, :include => :author).author end + def test_eager_association_loading_with_belongs_to_and_foreign_keys + pets = Pet.find(:all, :include => :owner) + assert_equal 3, pets.length + end + def test_eager_association_loading_with_belongs_to comments = Comment.find(:all, :include => :post) assert_equal 10, comments.length diff --git a/activerecord/test/fixtures/owners.yml b/activerecord/test/fixtures/owners.yml new file mode 100644 index 0000000000..d5493a84b7 --- /dev/null +++ b/activerecord/test/fixtures/owners.yml @@ -0,0 +1,7 @@ +blackbeard: + owner_id: 1 + name: blackbeard + +ashley: + owner_id: 2 + name: ashley diff --git a/activerecord/test/fixtures/pets.yml b/activerecord/test/fixtures/pets.yml new file mode 100644 index 0000000000..a1601a53f0 --- /dev/null +++ b/activerecord/test/fixtures/pets.yml @@ -0,0 +1,14 @@ +parrot: + pet_id: 1 + name: parrot + owner_id: 1 + +chew: + pet_id: 2 + name: chew + owner_id: 2 + +mochi: + pet_id: 3 + name: mochi + owner_id: 2 diff --git a/activerecord/test/models/owner.rb b/activerecord/test/models/owner.rb new file mode 100644 index 0000000000..dbaf2ce688 --- /dev/null +++ b/activerecord/test/models/owner.rb @@ -0,0 +1,4 @@ +class Owner < ActiveRecord::Base + set_primary_key :owner_id + has_many :pets +end
\ No newline at end of file diff --git a/activerecord/test/models/pet.rb b/activerecord/test/models/pet.rb new file mode 100644 index 0000000000..889ce46f33 --- /dev/null +++ b/activerecord/test/models/pet.rb @@ -0,0 +1,4 @@ +class Pet < ActiveRecord::Base + set_primary_key :pet_id + belongs_to :owner +end
\ No newline at end of file diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index bdf128aecc..8ab2022f8b 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -358,4 +358,13 @@ ActiveRecord::Schema.define do create_table 'warehouse-things', :force => true do |t| t.integer :value end + + create_table :owners, :primary_key => :owner_id ,:force => true do |t| + t.string :name + end + + create_table :pets, :primary_key => :pet_id ,:force => true do |t| + t.string :name + t.integer :owner_id, :integer + end end |