Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed :through relations when using STI inherited classes would use t…

…he 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
  • Loading branch information...
commit 5f06c483ca6a0434409aa12ec272654bc26735dd 1 parent 85fe1ec
Tobias Lütke authored
2  activerecord/CHANGELOG
View
@@ -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:
3  activerecord/lib/active_record/associations/has_many_through_association.rb
View
@@ -51,6 +51,7 @@ def find_target
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 @@ def construct_conditions
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]
15 activerecord/test/associations_join_model_test.rb
View
@@ -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 @@ def test_polymorphic_belongs_to
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
8 activerecord/test/fixtures/categorizations.yml
View
@@ -2,4 +2,10 @@ david_welcome_general:
id: 1
author_id: 1
post_id: 1
- category_id: 1
+ category_id: 1
+
+mary_thinking_sti:
+ id: 2
+ author_id: 2
+ post_id: 2
+ category_id: 3
3  activerecord/test/fixtures/category.rb
View
@@ -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
6 activerecord/test/fixtures/taggings.yml
View
@@ -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
6 activerecord/test/fixtures/tags.yml
View
@@ -1,3 +1,7 @@
general:
id: 1
- name: General
+ name: General
+
+misc:
+ id: 2
+ name: Misc
Please sign in to comment.
Something went wrong with that request. Please try again.