Browse files

Fixed preloading belongs_to associations which reference a custom for…

…eign key. References #9640.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8762 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent e59978a commit be827f9348a703b3048cb7728d0de6d019279bd9 @jeremy jeremy committed Jan 31, 2008
View
2 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]
View
3 activerecord/lib/active_record/association_preload.rb
@@ -180,14 +180,15 @@ def preload_belongs_to_association(records, reflection, preload_options={})
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],
:include => options[:include],
: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
View
10 activerecord/test/cases/associations/eager_test.rb
@@ -7,10 +7,13 @@
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 @@ def test_loading_with_no_associations
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
View
7 activerecord/test/fixtures/owners.yml
@@ -0,0 +1,7 @@
+blackbeard:
+ owner_id: 1
+ name: blackbeard
+
+ashley:
+ owner_id: 2
+ name: ashley
View
14 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
View
4 activerecord/test/models/owner.rb
@@ -0,0 +1,4 @@
+class Owner < ActiveRecord::Base
+ set_primary_key :owner_id
+ has_many :pets
+end
View
4 activerecord/test/models/pet.rb
@@ -0,0 +1,4 @@
+class Pet < ActiveRecord::Base
+ set_primary_key :pet_id
+ belongs_to :owner
+end
View
9 activerecord/test/schema/schema.rb
@@ -358,4 +358,13 @@ def create_table(*args, &block)
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

0 comments on commit be827f9

Please sign in to comment.