Skip to content
This repository
Browse code

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
... ... @@ -1,5 +1,7 @@
1 1 *SVN*
2 2
  3 +* Fixed :through relations when using STI inherited classes would use the inherited class's name as foreign key on the join model [Tobias Luetke]
  4 +
3 5 * MySQL: allow encoding option for mysql.rb driver. [Jeremy Kemper]
4 6
5 7 * 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
@@ -51,6 +51,7 @@ def find_target
51 51
52 52 def construct_conditions
53 53 through_reflection = @owner.class.reflections[@reflection.options[:through]]
  54 + through_foreign_classname = ActiveRecord::Base.send(:class_name_of_active_record_descendant, @owner.class).to_s
54 55
55 56 if through_reflection.options[:as]
56 57 conditions =
@@ -60,7 +61,7 @@ def construct_conditions
60 61 else
61 62 conditions =
62 63 "#{@reflection.klass.table_name}.#{@reflection.klass.primary_key} = #{through_reflection.table_name}.#{@reflection.klass.to_s.foreign_key} " +
63   - "AND #{through_reflection.table_name}.#{@owner.class.to_s.foreign_key} = #{@owner.quoted_id}"
  64 + "AND #{through_reflection.table_name}.#{through_foreign_classname.foreign_key} = #{@owner.quoted_id}"
64 65 end
65 66
66 67 conditions << " AND (#{interpolate_sql(sanitize_sql(@reflection.options[:conditions]))})" if @reflection.options[:conditions]
15 activerecord/test/associations_join_model_test.rb
@@ -14,7 +14,15 @@ class AssociationsJoinModelTest < Test::Unit::TestCase
14 14 def test_has_many
15 15 assert_equal categories(:general), authors(:david).categories.first
16 16 end
  17 +
  18 + def test_has_many_inherited
  19 + assert_equal categories(:sti_test), authors(:mary).categories.first
  20 + end
17 21
  22 + def test_inherited_has_many
  23 + assert_equal authors(:mary), categories(:sti_test).authors.first
  24 + end
  25 +
18 26 def test_polymorphic_has_many
19 27 assert_equal taggings(:welcome_general), posts(:welcome).taggings.first
20 28 end
@@ -25,5 +33,10 @@ def test_polymorphic_belongs_to
25 33
26 34 def test_polymorphic_has_many_going_through_join_model
27 35 assert_equal tags(:general), posts(:welcome).tags.first
28   - end
  36 + end
  37 +
  38 + def test_polymorphic_has_many_going_through_join_model_with_inheritance
  39 + assert_equal tags(:general), posts(:thinking).tags.first
  40 + end
  41 +
29 42 end
8 activerecord/test/fixtures/categorizations.yml
@@ -2,4 +2,10 @@ david_welcome_general:
2 2 id: 1
3 3 author_id: 1
4 4 post_id: 1
5   - category_id: 1
  5 + category_id: 1
  6 +
  7 +mary_thinking_sti:
  8 + id: 2
  9 + author_id: 2
  10 + post_id: 2
  11 + category_id: 3
3  activerecord/test/fixtures/category.rb
@@ -4,6 +4,9 @@ class Category < ActiveRecord::Base
4 4 def self.what_are_you
5 5 'a category...'
6 6 end
  7 +
  8 + has_many :categorizations
  9 + has_many :authors, :through => :categorizations
7 10 end
8 11
9 12 class SpecialCategory < Category
6 activerecord/test/fixtures/taggings.yml
@@ -3,3 +3,9 @@ welcome_general:
3 3 tag_id: 1
4 4 taggable_id: 1
5 5 taggable_type: Post
  6 +
  7 +thinking_general:
  8 + id: 2
  9 + tag_id: 1
  10 + taggable_id: 2
  11 + taggable_type: Post
6 activerecord/test/fixtures/tags.yml
... ... @@ -1,3 +1,7 @@
1 1 general:
2 2 id: 1
3   - name: General
  3 + name: General
  4 +
  5 +misc:
  6 + id: 2
  7 + name: Misc

0 comments on commit 5f06c48

Please sign in to comment.
Something went wrong with that request. Please try again.