提交 c83f28ff 编写于 作者: R Ryuta Kamizono

Ensure `AliasTracker` respects a custom table name

上级 5349f231
......@@ -23,7 +23,7 @@ def scope(association)
reflection = association.reflection
scope = klass.unscoped
owner = association.owner
alias_tracker = AliasTracker.create(klass.connection, klass.table_name)
alias_tracker = AliasTracker.create(klass.connection, scope.table.name)
chain = get_chain(reflection, association, alias_tracker)
scope.extending! reflection.extensions
......
......@@ -91,7 +91,7 @@ def self.walk_tree(associations, hash)
# joins # => []
#
def initialize(base, table, associations, joins, eager_loading: true)
@alias_tracker = AliasTracker.create_with_joins(base.connection, base.table_name, joins)
@alias_tracker = AliasTracker.create_with_joins(base.connection, table.name, joins)
@eager_loading = eager_loading
tree = self.class.make_tree associations
@join_root = JoinBase.new(base, table, build(tree, base))
......
......@@ -19,12 +19,12 @@
require "models/minivan"
require "models/possession"
require "models/reader"
require "models/category"
require "models/categorization"
require "models/edge"
class RelationTest < ActiveRecord::TestCase
fixtures :authors, :author_addresses, :topics, :entrants, :developers, :companies, :developers_projects, :accounts, :categories, :categorizations, :posts, :comments,
:tags, :taggings, :cars, :minivans
fixtures :authors, :author_addresses, :topics, :entrants, :developers, :companies, :developers_projects, :accounts, :categories, :categorizations, :categories_posts, :posts, :comments, :tags, :taggings, :cars, :minivans
class TopicWithCallbacks < ActiveRecord::Base
self.table_name = :topics
......@@ -1810,6 +1810,10 @@ def test_presence
assert_equal [posts(:welcome)], custom_post_relation.ranked_by_comments.limit_by(1).to_a
end
test "alias_tracker respects a custom table" do
assert_equal posts(:welcome), custom_post_relation("categories_posts").joins(:categories).first
end
test "#load" do
relation = Post.all
assert_queries(1) do
......@@ -1930,8 +1934,8 @@ def test_relation_join_method
end
private
def custom_post_relation
table_alias = Post.arel_table.alias("omg_posts")
def custom_post_relation(alias_name = "omg_posts")
table_alias = Post.arel_table.alias(alias_name)
table_metadata = ActiveRecord::TableMetadata.new(Post, table_alias)
predicate_builder = ActiveRecord::PredicateBuilder.new(table_metadata)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册