1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
This commit is contained in:
Tobias Lütke 2005-12-16 23:24:58 +00:00
parent 85fe1ecaef
commit 5f06c483ca
7 changed files with 39 additions and 4 deletions

View file

@ -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:

View file

@ -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]

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -1,3 +1,7 @@
general:
id: 1
name: General
name: General
misc:
id: 2
name: Misc