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:
parent
85fe1ecaef
commit
5f06c483ca
7 changed files with 39 additions and 4 deletions
|
@ -1,5 +1,7 @@
|
||||||
*SVN*
|
*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]
|
* 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:
|
* Added option inheritance for find calls on has_and_belongs_to_many and has_many assosociations [DHH]. Example:
|
||||||
|
|
|
@ -51,6 +51,7 @@ module ActiveRecord
|
||||||
|
|
||||||
def construct_conditions
|
def construct_conditions
|
||||||
through_reflection = @owner.class.reflections[@reflection.options[:through]]
|
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]
|
if through_reflection.options[:as]
|
||||||
conditions =
|
conditions =
|
||||||
|
@ -60,7 +61,7 @@ module ActiveRecord
|
||||||
else
|
else
|
||||||
conditions =
|
conditions =
|
||||||
"#{@reflection.klass.table_name}.#{@reflection.klass.primary_key} = #{through_reflection.table_name}.#{@reflection.klass.to_s.foreign_key} " +
|
"#{@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
|
end
|
||||||
|
|
||||||
conditions << " AND (#{interpolate_sql(sanitize_sql(@reflection.options[:conditions]))})" if @reflection.options[:conditions]
|
conditions << " AND (#{interpolate_sql(sanitize_sql(@reflection.options[:conditions]))})" if @reflection.options[:conditions]
|
||||||
|
|
|
@ -14,7 +14,15 @@ class AssociationsJoinModelTest < Test::Unit::TestCase
|
||||||
def test_has_many
|
def test_has_many
|
||||||
assert_equal categories(:general), authors(:david).categories.first
|
assert_equal categories(:general), authors(:david).categories.first
|
||||||
end
|
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
|
def test_polymorphic_has_many
|
||||||
assert_equal taggings(:welcome_general), posts(:welcome).taggings.first
|
assert_equal taggings(:welcome_general), posts(:welcome).taggings.first
|
||||||
end
|
end
|
||||||
|
@ -25,5 +33,10 @@ class AssociationsJoinModelTest < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_polymorphic_has_many_going_through_join_model
|
def test_polymorphic_has_many_going_through_join_model
|
||||||
assert_equal tags(:general), posts(:welcome).tags.first
|
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
|
end
|
||||||
|
|
|
@ -2,4 +2,10 @@ david_welcome_general:
|
||||||
id: 1
|
id: 1
|
||||||
author_id: 1
|
author_id: 1
|
||||||
post_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
vendored
3
activerecord/test/fixtures/category.rb
vendored
|
@ -4,6 +4,9 @@ class Category < ActiveRecord::Base
|
||||||
def self.what_are_you
|
def self.what_are_you
|
||||||
'a category...'
|
'a category...'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
has_many :categorizations
|
||||||
|
has_many :authors, :through => :categorizations
|
||||||
end
|
end
|
||||||
|
|
||||||
class SpecialCategory < Category
|
class SpecialCategory < Category
|
||||||
|
|
6
activerecord/test/fixtures/taggings.yml
vendored
6
activerecord/test/fixtures/taggings.yml
vendored
|
@ -3,3 +3,9 @@ welcome_general:
|
||||||
tag_id: 1
|
tag_id: 1
|
||||||
taggable_id: 1
|
taggable_id: 1
|
||||||
taggable_type: Post
|
taggable_type: Post
|
||||||
|
|
||||||
|
thinking_general:
|
||||||
|
id: 2
|
||||||
|
tag_id: 1
|
||||||
|
taggable_id: 2
|
||||||
|
taggable_type: Post
|
||||||
|
|
6
activerecord/test/fixtures/tags.yml
vendored
6
activerecord/test/fixtures/tags.yml
vendored
|
@ -1,3 +1,7 @@
|
||||||
general:
|
general:
|
||||||
id: 1
|
id: 1
|
||||||
name: General
|
name: General
|
||||||
|
|
||||||
|
misc:
|
||||||
|
id: 2
|
||||||
|
name: Misc
|
Loading…
Reference in a new issue