From ac78d3c6326c37c50eb4d4b49ec60ad435bd6846 Mon Sep 17 00:00:00 2001 From: Justin Marney Date: Thu, 31 Jul 2008 16:30:19 -0400 Subject: [PATCH] hmt with custom primary keys --- .../associations/has_many_through_association.rb | 2 +- .../has_many_through_associations_test.rb | 12 +++++++++--- activerecord/test/fixtures/bikes.yml | 11 +++++++++++ activerecord/test/fixtures/spokes.yml | 11 +++++++++++ activerecord/test/models/bike.rb | 5 +++++ activerecord/test/models/owner.rb | 2 ++ activerecord/test/models/spoke.rb | 4 ++++ activerecord/test/schema/schema.rb | 8 ++++++++ 8 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 activerecord/test/fixtures/bikes.yml create mode 100644 activerecord/test/fixtures/spokes.yml create mode 100644 activerecord/test/models/bike.rb create mode 100644 activerecord/test/models/spoke.rb 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 e1bfff5..24c5dcd 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -148,7 +148,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 d51a3c7..f7a73ed 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -3,15 +3,21 @@ require 'models/post' require 'models/person' require 'models/reader' require 'models/comment' +require 'models/owner' +require 'models/bike' +require 'models/spoke' class HasManyThroughAssociationsTest < ActiveRecord::TestCase - fixtures :posts, :readers, :people, :comments + fixtures :posts, :readers, :people, :owners, :bikes, :spokes + + def test_association_with_custom_primary_keys + assert_equal owners(:blackbeard).spokes, [spokes(:uno)] + assert_equal owners(:ashley).spokes, [spokes(:dos), spokes(:tres)] + end def test_associate_existing assert_queries(2) { posts(:thinking);people(:david) } - posts(:thinking).people - assert_queries(1) do posts(:thinking).people << people(:david) end diff --git a/activerecord/test/fixtures/bikes.yml b/activerecord/test/fixtures/bikes.yml new file mode 100644 index 0000000..81e103b --- /dev/null +++ b/activerecord/test/fixtures/bikes.yml @@ -0,0 +1,11 @@ +surly: + bike_id: 1 + owner_id: 1 + +independent: + bike_id: 2 + owner_id: 2 + +soulcraft: + bike_id: 3 + owner_id: 2 diff --git a/activerecord/test/fixtures/spokes.yml b/activerecord/test/fixtures/spokes.yml new file mode 100644 index 0000000..7f6a196 --- /dev/null +++ b/activerecord/test/fixtures/spokes.yml @@ -0,0 +1,11 @@ +uno: + spoke_id: 1 + bike_id: 1 + +dos: + spoke_id: 2 + bike_id: 2 + +tres: + spoke_id: 3 + bike_id: 2 diff --git a/activerecord/test/models/bike.rb b/activerecord/test/models/bike.rb new file mode 100644 index 0000000..11a0025 --- /dev/null +++ b/activerecord/test/models/bike.rb @@ -0,0 +1,5 @@ +class Bike < ActiveRecord::Base + set_primary_key :bike_id + belongs_to :owner + has_many :spokes +end \ No newline at end of file diff --git a/activerecord/test/models/owner.rb b/activerecord/test/models/owner.rb index dbaf2ce..a05fd24 100644 --- a/activerecord/test/models/owner.rb +++ b/activerecord/test/models/owner.rb @@ -1,4 +1,6 @@ class Owner < ActiveRecord::Base set_primary_key :owner_id has_many :pets + has_many :bikes + has_many :spokes, :through => :bikes end \ No newline at end of file diff --git a/activerecord/test/models/spoke.rb b/activerecord/test/models/spoke.rb new file mode 100644 index 0000000..289ddd3 --- /dev/null +++ b/activerecord/test/models/spoke.rb @@ -0,0 +1,4 @@ +class Spoke < ActiveRecord::Base + set_primary_key :spoke_id + belongs_to :bike +end \ No newline at end of file diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 487a08f..14cd689 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -51,6 +51,10 @@ ActiveRecord::Schema.define do t.integer :value end + create_table :bikes, :primary_key => :bike_id, :force => true do |t| + t.integer :owner_id + end + create_table :binaries, :force => true do |t| t.binary :data end @@ -339,6 +343,10 @@ ActiveRecord::Schema.define do t.datetime :updated_on end + create_table :spokes, :primary_key => :spoke_id, :force => true do |t| + t.integer :bike_id + end + create_table :sponsors, :force => true do |t| t.integer :club_id t.integer :sponsorable_id -- 1.5.6.rc2.26.g8c37