diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 433ec57e23..eca074f324 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fix that has_many :through honors the foreign key set by the belongs_to association in the join model (closes #4259) [andylien@gmail.com / Rick] + * SQL Server adapter gets some love #4298 [rtomayko@gmail.com] * Added OpenBase database adapter that builds on top of the http://www.spice-of-life.net/ruby-openbase/ driver. All functionality except LIMIT/OFFSET is supported #3528 [derrickspell@cdmplus.com] 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 e242b9f7a1..3d3d53c677 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -65,14 +65,17 @@ module ActiveRecord raise ActiveRecordError, "Could not find the association '#{@reflection.options[:through]}' in model #{@reflection.klass}" end + # Get the actual primary key of the belongs_to association that the reflection is going through + source_primary_key = through_reflection.klass.reflect_on_association(@reflection.name.to_s.singularize.to_sym).primary_key_name + if through_reflection.options[:as] conditions = - "#{@reflection.table_name}.#{@reflection.klass.primary_key} = #{through_reflection.table_name}.#{@reflection.klass.to_s.foreign_key} " + + "#{@reflection.table_name}.#{@reflection.klass.primary_key} = #{through_reflection.table_name}.#{source_primary_key} " + "AND #{through_reflection.table_name}.#{through_reflection.options[:as]}_id = #{@owner.quoted_id} " + "AND #{through_reflection.table_name}.#{through_reflection.options[:as]}_type = #{@owner.class.quote @owner.class.base_class.name.to_s}" else conditions = - "#{@reflection.klass.table_name}.#{@reflection.klass.primary_key} = #{through_reflection.table_name}.#{@reflection.klass.to_s.foreign_key} " + + "#{@reflection.klass.table_name}.#{@reflection.klass.primary_key} = #{through_reflection.table_name}.#{source_primary_key} " + "AND #{through_reflection.table_name}.#{through_reflection.primary_key_name} = #{@owner.quoted_id}" end diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index e54e38e184..e56682ec92 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -140,6 +140,10 @@ module ActiveRecord end end + def through_reflection + @through_reflection ||= options[:through] ? active_record.reflect_on_association(options[:through]) : false + end + private def name_to_class_name(name) if name =~ /::/ @@ -147,6 +151,8 @@ module ActiveRecord else if options[:class_name] options[:class_name] + elsif through_reflection # get the class_name of the belongs_to association of the through reflection + through_reflection.klass.reflect_on_association(name.to_s.singularize.to_sym).class_name else class_name = name.to_s.camelize class_name = class_name.singularize if [ :has_many, :has_and_belongs_to_many ].include?(macro) diff --git a/activerecord/test/associations_join_model_test.rb b/activerecord/test/associations_join_model_test.rb index 7395aa5739..79ac19ec61 100644 --- a/activerecord/test/associations_join_model_test.rb +++ b/activerecord/test/associations_join_model_test.rb @@ -39,6 +39,11 @@ class AssociationsJoinModelTest < Test::Unit::TestCase assert_equal tags(:general), posts(:welcome).tags.first end + def test_polymorphic_has_many_going_through_join_model_with_custom_foreign_key + assert_equal tags(:misc), taggings(:welcome_general).super_tag + assert_equal tags(:misc), posts(:welcome).super_tags.first + end + def test_polymorphic_has_many_create_model_with_inheritance_and_custom_base_class post = SubStiPost.create :title => 'SubStiPost', :body => 'SubStiPost body' assert_instance_of SubStiPost, post diff --git a/activerecord/test/fixtures/db_definitions/schema.rb b/activerecord/test/fixtures/db_definitions/schema.rb index 53aa7c1967..efe9cb35ab 100644 --- a/activerecord/test/fixtures/db_definitions/schema.rb +++ b/activerecord/test/fixtures/db_definitions/schema.rb @@ -2,6 +2,7 @@ ActiveRecord::Schema.define do create_table :taggings, :force => true do |t| t.column :tag_id, :integer + t.column :super_tag_id, :integer t.column :taggable_type, :string t.column :taggable_id, :integer end diff --git a/activerecord/test/fixtures/post.rb b/activerecord/test/fixtures/post.rb index a00fd80671..9456582729 100644 --- a/activerecord/test/fixtures/post.rb +++ b/activerecord/test/fixtures/post.rb @@ -22,6 +22,7 @@ class Post < ActiveRecord::Base has_many :taggings, :as => :taggable has_many :tags, :through => :taggings + has_many :super_tags, :through => :taggings has_one :tagging, :as => :taggable diff --git a/activerecord/test/fixtures/tagging.rb b/activerecord/test/fixtures/tagging.rb index 8bc844c370..4d3878933a 100644 --- a/activerecord/test/fixtures/tagging.rb +++ b/activerecord/test/fixtures/tagging.rb @@ -1,4 +1,5 @@ class Tagging < ActiveRecord::Base belongs_to :tag + belongs_to :super_tag, :class_name => 'Tag', :foreign_key => 'super_tag_id' belongs_to :taggable, :polymorphic => true, :counter_cache => true end \ No newline at end of file diff --git a/activerecord/test/fixtures/taggings.yml b/activerecord/test/fixtures/taggings.yml index 38ec1ba5b1..617210d604 100644 --- a/activerecord/test/fixtures/taggings.yml +++ b/activerecord/test/fixtures/taggings.yml @@ -1,6 +1,7 @@ welcome_general: id: 1 tag_id: 1 + super_tag_id: 2 taggable_id: 1 taggable_type: Post