diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 23fe76de2e..1c091e7d5a 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -150,7 +150,7 @@ module ActiveRecord end else reflection_primary_key = @reflection.source_reflection.primary_key_name - source_primary_key = @reflection.klass.primary_key + source_primary_key = @reflection.through_reflection.klass.primary_key if @reflection.source_reflection.options[:as] polymorphic_join = "AND %s.%s = %s" % [ @reflection.quoted_table_name, "#{@reflection.source_reflection.options[:as]}_type", diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index ac00508b5f..c3ad0ee6de 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -8,9 +8,12 @@ require 'models/comment' require 'models/tag' require 'models/tagging' require 'models/author' +require 'models/owner' +require 'models/pet' +require 'models/toy' class HasManyThroughAssociationsTest < ActiveRecord::TestCase - fixtures :posts, :readers, :people, :comments, :authors + fixtures :posts, :readers, :people, :comments, :authors, :owners, :pets, :toys def test_associate_existing assert_queries(2) { posts(:thinking);people(:david) } @@ -251,4 +254,8 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase author.author_favorites.create(:favorite_author_id => 3) assert_equal post.author.author_favorites, post.author_favorites end + + def test_has_many_association_through_a_has_many_association_with_nonstandard_primary_keys + assert_equal 1, owners(:blackbeard).toys.count + end end diff --git a/activerecord/test/fixtures/toys.yml b/activerecord/test/fixtures/toys.yml new file mode 100644 index 0000000000..037e335e0a --- /dev/null +++ b/activerecord/test/fixtures/toys.yml @@ -0,0 +1,4 @@ +bone: + toy_id: 1 + name: Bone + pet_id: 1 diff --git a/activerecord/test/models/owner.rb b/activerecord/test/models/owner.rb index dbaf2ce688..5760b991ec 100644 --- a/activerecord/test/models/owner.rb +++ b/activerecord/test/models/owner.rb @@ -1,4 +1,5 @@ class Owner < ActiveRecord::Base set_primary_key :owner_id has_many :pets -end \ No newline at end of file + has_many :toys, :through => :pets +end diff --git a/activerecord/test/models/pet.rb b/activerecord/test/models/pet.rb index 889ce46f33..dc1a3c5e94 100644 --- a/activerecord/test/models/pet.rb +++ b/activerecord/test/models/pet.rb @@ -1,4 +1,5 @@ class Pet < ActiveRecord::Base set_primary_key :pet_id belongs_to :owner -end \ No newline at end of file + has_many :toys +end diff --git a/activerecord/test/models/toy.rb b/activerecord/test/models/toy.rb new file mode 100644 index 0000000000..79a88db0da --- /dev/null +++ b/activerecord/test/models/toy.rb @@ -0,0 +1,4 @@ +class Toy < ActiveRecord::Base + set_primary_key :toy_id + belongs_to :pet +end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 9ebf1d82be..ea848a2940 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -425,6 +425,11 @@ ActiveRecord::Schema.define do t.column :taggings_count, :integer, :default => 0 end + create_table :toys, :primary_key => :toy_id ,:force => true do |t| + t.string :name + t.integer :pet_id, :integer + end + create_table :treasures, :force => true do |t| t.column :name, :string t.column :looter_id, :integer