aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb3
-rw-r--r--activerecord/test/associations_join_model_test.rb15
-rw-r--r--activerecord/test/fixtures/categorizations.yml8
-rw-r--r--activerecord/test/fixtures/category.rb3
-rw-r--r--activerecord/test/fixtures/taggings.yml6
-rw-r--r--activerecord/test/fixtures/tags.yml6
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