Skip to content
Browse files

Ensure has_many :through works with changed primary keys [#736 state:…

…resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
1 parent 1ab2ff5 commit 1e6c50e21bdb8c99116a7dc6921ef3eb4ed9531a Jim Remsik and Tim Pope committed with lifo Mar 9, 2009
View
2 activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -150,7 +150,7 @@ def construct_joins(custom_joins = nil)
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",
View
9 activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -8,9 +8,12 @@
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 @@ def test_has_many_association_through_a_belongs_to_association
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
View
4 activerecord/test/fixtures/toys.yml
@@ -0,0 +1,4 @@
+bone:
+ toy_id: 1
+ name: Bone
+ pet_id: 1
View
3 activerecord/test/models/owner.rb
@@ -1,4 +1,5 @@
class Owner < ActiveRecord::Base
set_primary_key :owner_id
has_many :pets
-end
+ has_many :toys, :through => :pets
+end
View
3 activerecord/test/models/pet.rb
@@ -1,4 +1,5 @@
class Pet < ActiveRecord::Base
set_primary_key :pet_id
belongs_to :owner
-end
+ has_many :toys
+end
View
4 activerecord/test/models/toy.rb
@@ -0,0 +1,4 @@
+class Toy < ActiveRecord::Base
+ set_primary_key :toy_id
+ belongs_to :pet
+end
View
5 activerecord/test/schema/schema.rb
@@ -425,6 +425,11 @@ def create_table(*args, &block)
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

0 comments on commit 1e6c50e

Please sign in to comment.
Something went wrong with that request. Please try again.