aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2017-09-30 20:07:09 +0900
committerRyuta Kamizono <kamipo@gmail.com>2017-09-30 20:07:09 +0900
commitc83f28fffe8b18ee75822394a11f446c05f692fd (patch)
tree85c65a0c0e94f2e29dab82c5ae486e8283690130 /activerecord
parent5349f231089ddb884c7cb5405e74b7871383d65d (diff)
downloadrails-c83f28fffe8b18ee75822394a11f446c05f692fd.tar.gz
rails-c83f28fffe8b18ee75822394a11f446c05f692fd.tar.bz2
rails-c83f28fffe8b18ee75822394a11f446c05f692fd.zip
Ensure `AliasTracker` respects a custom table name
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/association_scope.rb2
-rw-r--r--activerecord/lib/active_record/associations/join_dependency.rb2
-rw-r--r--activerecord/test/cases/relations_test.rb12
3 files changed, 10 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb
index b2dc044312..6118cef913 100644
--- a/activerecord/lib/active_record/associations/association_scope.rb
+++ b/activerecord/lib/active_record/associations/association_scope.rb
@@ -23,7 +23,7 @@ module ActiveRecord
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
diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb
index 77b3d11b47..23741b2f6a 100644
--- a/activerecord/lib/active_record/associations/join_dependency.rb
+++ b/activerecord/lib/active_record/associations/join_dependency.rb
@@ -91,7 +91,7 @@ module ActiveRecord
# 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))
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 4edaf79e9a..e1788be351 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -19,12 +19,12 @@ require "models/tyre"
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 @@ class RelationTest < ActiveRecord::TestCase
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 @@ class RelationTest < ActiveRecord::TestCase
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)