aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJim Remsik and Tim Pope <dev+bigtiger+tpope@hashrocket.com>2009-03-09 13:42:51 +0000
committerPratik Naik <pratiknaik@gmail.com>2009-03-09 13:43:28 +0000
commit1e6c50e21bdb8c99116a7dc6921ef3eb4ed9531a (patch)
tree1658deea2328ef01332fd8589dfaa461f4186d87
parent1ab2ff58eddee8ac523624dd097424f9a760ad8b (diff)
downloadrails-1e6c50e21bdb8c99116a7dc6921ef3eb4ed9531a.tar.gz
rails-1e6c50e21bdb8c99116a7dc6921ef3eb4ed9531a.tar.bz2
rails-1e6c50e21bdb8c99116a7dc6921ef3eb4ed9531a.zip
Ensure has_many :through works with changed primary keys [#736 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb2
-rw-r--r--activerecord/test/cases/associations/has_many_through_associations_test.rb9
-rw-r--r--activerecord/test/fixtures/toys.yml4
-rw-r--r--activerecord/test/models/owner.rb3
-rw-r--r--activerecord/test/models/pet.rb3
-rw-r--r--activerecord/test/models/toy.rb4
-rw-r--r--activerecord/test/schema/schema.rb5
7 files changed, 26 insertions, 4 deletions
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