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

Association creation and finding should work consistently (#32048)

This is an alternative of #29722, and revert of #29601 and a1fcbd9.

Currently, association creation and normal association finding doesn't
respect `store_full_sti_class`. But eager loading and preloading respect
the setting. This means that if set `store_full_sti_class = false`
(`true` by default), eager loading and preloading can not find
created polymorphic records.

Association creation and finding should work consistently.
This commit is contained in:
Ryuta Kamizono 2018-02-26 05:09:22 +09:00 committed by GitHub
parent dccdcfb5a3
commit 8ff70cad3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 7 deletions

View file

@ -117,7 +117,7 @@ module ActiveRecord
scope = klass.scope_for_association
if reflection.type
scope.where!(reflection.type => model.base_class.sti_name)
scope.where!(reflection.type => model.base_class.name)
end
scope.merge!(reflection_scope) if reflection.scope

View file

@ -193,7 +193,7 @@ module ActiveRecord
klass_scope = klass_join_scope(table, predicate_builder)
if type
klass_scope.where!(type => foreign_klass.base_class.sti_name)
klass_scope.where!(type => foreign_klass.base_class.name)
end
scope_chain_items.inject(klass_scope, &:merge!)

View file

@ -11,21 +11,33 @@ module Namespaced
end
end
class EagerLoadIncludeFullStiClassNamesTest < ActiveRecord::TestCase
module PolymorphicFullStiClassNamesSharedTest
def setup
@old_store_full_sti_class = ActiveRecord::Base.store_full_sti_class
ActiveRecord::Base.store_full_sti_class = store_full_sti_class
post = Namespaced::Post.create(title: "Great stuff", body: "This is not", author_id: 1)
@tagging = Tagging.create(taggable: post)
@old = ActiveRecord::Base.store_full_sti_class
end
def teardown
ActiveRecord::Base.store_full_sti_class = @old
ActiveRecord::Base.store_full_sti_class = @old_store_full_sti_class
end
def test_class_names
ActiveRecord::Base.store_full_sti_class = false
post = Namespaced::Post.find_by_title("Great stuff")
assert_equal @tagging, post.tagging
ActiveRecord::Base.store_full_sti_class = true
post = Namespaced::Post.find_by_title("Great stuff")
assert_equal @tagging, post.tagging
end
def test_class_names_with_includes
ActiveRecord::Base.store_full_sti_class = false
post = Namespaced::Post.includes(:tagging).find_by_title("Great stuff")
assert_nil post.tagging
assert_equal @tagging, post.tagging
ActiveRecord::Base.store_full_sti_class = true
post = Namespaced::Post.includes(:tagging).find_by_title("Great stuff")
@ -35,10 +47,28 @@ class EagerLoadIncludeFullStiClassNamesTest < ActiveRecord::TestCase
def test_class_names_with_eager_load
ActiveRecord::Base.store_full_sti_class = false
post = Namespaced::Post.eager_load(:tagging).find_by_title("Great stuff")
assert_nil post.tagging
assert_equal @tagging, post.tagging
ActiveRecord::Base.store_full_sti_class = true
post = Namespaced::Post.eager_load(:tagging).find_by_title("Great stuff")
assert_equal @tagging, post.tagging
end
end
class PolymorphicFullStiClassNamesTest < ActiveRecord::TestCase
include PolymorphicFullStiClassNamesSharedTest
private
def store_full_sti_class
true
end
end
class PolymorphicNonFullStiClassNamesTest < ActiveRecord::TestCase
include PolymorphicFullStiClassNamesSharedTest
private
def store_full_sti_class
false
end
end