diff options
author | Tobias Lütke <tobias.luetke@gmail.com> | 2005-12-16 23:24:58 +0000 |
---|---|---|
committer | Tobias Lütke <tobias.luetke@gmail.com> | 2005-12-16 23:24:58 +0000 |
commit | 5f06c483ca6a0434409aa12ec272654bc26735dd (patch) | |
tree | 4d4e05b12d3aa659550e972f65ae5e2712869ff8 | |
parent | 85fe1ecaefe415ac1de36883f5f6162f49bd5287 (diff) | |
download | rails-5f06c483ca6a0434409aa12ec272654bc26735dd.tar.gz rails-5f06c483ca6a0434409aa12ec272654bc26735dd.tar.bz2 rails-5f06c483ca6a0434409aa12ec272654bc26735dd.zip |
Fixed :through relations when using STI inherited classes would use the inherited class's name as foreign key on the join model
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3315 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_through_association.rb | 3 | ||||
-rw-r--r-- | activerecord/test/associations_join_model_test.rb | 15 | ||||
-rw-r--r-- | activerecord/test/fixtures/categorizations.yml | 8 | ||||
-rw-r--r-- | activerecord/test/fixtures/category.rb | 3 | ||||
-rw-r--r-- | activerecord/test/fixtures/taggings.yml | 6 | ||||
-rw-r--r-- | activerecord/test/fixtures/tags.yml | 6 |
7 files changed, 39 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index df060b1c6d..de9e986861 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed :through relations when using STI inherited classes would use the inherited class's name as foreign key on the join model [Tobias Luetke] + * MySQL: allow encoding option for mysql.rb driver. [Jeremy Kemper] * Added option inheritance for find calls on has_and_belongs_to_many and has_many assosociations [DHH]. Example: 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 f6ac03a3e6..8c91aeeed4 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -51,6 +51,7 @@ module ActiveRecord def construct_conditions through_reflection = @owner.class.reflections[@reflection.options[:through]] + through_foreign_classname = ActiveRecord::Base.send(:class_name_of_active_record_descendant, @owner.class).to_s if through_reflection.options[:as] conditions = @@ -60,7 +61,7 @@ module ActiveRecord else conditions = "#{@reflection.klass.table_name}.#{@reflection.klass.primary_key} = #{through_reflection.table_name}.#{@reflection.klass.to_s.foreign_key} " + - "AND #{through_reflection.table_name}.#{@owner.class.to_s.foreign_key} = #{@owner.quoted_id}" + "AND #{through_reflection.table_name}.#{through_foreign_classname.foreign_key} = #{@owner.quoted_id}" end conditions << " AND (#{interpolate_sql(sanitize_sql(@reflection.options[:conditions]))})" if @reflection.options[:conditions] diff --git a/activerecord/test/associations_join_model_test.rb b/activerecord/test/associations_join_model_test.rb index 7b53141ccd..d53a8d98d3 100644 --- a/activerecord/test/associations_join_model_test.rb +++ b/activerecord/test/associations_join_model_test.rb @@ -14,7 +14,15 @@ class AssociationsJoinModelTest < Test::Unit::TestCase def test_has_many assert_equal categories(:general), authors(:david).categories.first end + + def test_has_many_inherited + assert_equal categories(:sti_test), authors(:mary).categories.first + end + def test_inherited_has_many + assert_equal authors(:mary), categories(:sti_test).authors.first + end + def test_polymorphic_has_many assert_equal taggings(:welcome_general), posts(:welcome).taggings.first end @@ -25,5 +33,10 @@ class AssociationsJoinModelTest < Test::Unit::TestCase def test_polymorphic_has_many_going_through_join_model assert_equal tags(:general), posts(:welcome).tags.first - end + end + + def test_polymorphic_has_many_going_through_join_model_with_inheritance + assert_equal tags(:general), posts(:thinking).tags.first + end + end diff --git a/activerecord/test/fixtures/categorizations.yml b/activerecord/test/fixtures/categorizations.yml index a93fd6b79e..f8701fbde0 100644 --- a/activerecord/test/fixtures/categorizations.yml +++ b/activerecord/test/fixtures/categorizations.yml @@ -2,4 +2,10 @@ david_welcome_general: id: 1 author_id: 1 post_id: 1 - category_id: 1
\ No newline at end of file + category_id: 1 + +mary_thinking_sti: + id: 2 + author_id: 2 + post_id: 2 + category_id: 3
\ No newline at end of file diff --git a/activerecord/test/fixtures/category.rb b/activerecord/test/fixtures/category.rb index 880eb1573d..01938d1405 100644 --- a/activerecord/test/fixtures/category.rb +++ b/activerecord/test/fixtures/category.rb @@ -4,6 +4,9 @@ class Category < ActiveRecord::Base def self.what_are_you 'a category...' end + + has_many :categorizations + has_many :authors, :through => :categorizations end class SpecialCategory < Category diff --git a/activerecord/test/fixtures/taggings.yml b/activerecord/test/fixtures/taggings.yml index ca171346f1..dced625580 100644 --- a/activerecord/test/fixtures/taggings.yml +++ b/activerecord/test/fixtures/taggings.yml @@ -3,3 +3,9 @@ welcome_general: tag_id: 1 taggable_id: 1 taggable_type: Post + +thinking_general: + id: 2 + tag_id: 1 + taggable_id: 2 + taggable_type: Post diff --git a/activerecord/test/fixtures/tags.yml b/activerecord/test/fixtures/tags.yml index 2a494089ff..471b96f362 100644 --- a/activerecord/test/fixtures/tags.yml +++ b/activerecord/test/fixtures/tags.yml @@ -1,3 +1,7 @@ general: id: 1 - name: General
\ No newline at end of file + name: General + +misc: + id: 2 + name: Misc
\ No newline at end of file |