mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #29828 from kamipo/fix_using_custom_table_with_joins
Fix `JoinDependency` with using a custom table
This commit is contained in:
commit
589adea81c
6 changed files with 31 additions and 20 deletions
|
@ -88,11 +88,11 @@ module ActiveRecord
|
|||
# associations # => [:appointments]
|
||||
# joins # => []
|
||||
#
|
||||
def initialize(base, associations, joins, eager_loading: true)
|
||||
def initialize(base, table, associations, joins, eager_loading: true)
|
||||
@alias_tracker = AliasTracker.create_with_joins(base.connection, base.table_name, joins)
|
||||
@eager_loading = eager_loading
|
||||
tree = self.class.make_tree associations
|
||||
@join_root = JoinBase.new base, build(tree, base)
|
||||
@join_root = JoinBase.new(base, table, build(tree, base))
|
||||
@join_root.children.each { |child| construct_tables! @join_root, child }
|
||||
end
|
||||
|
||||
|
|
|
@ -4,14 +4,17 @@ module ActiveRecord
|
|||
module Associations
|
||||
class JoinDependency # :nodoc:
|
||||
class JoinBase < JoinPart # :nodoc:
|
||||
attr_reader :table
|
||||
|
||||
def initialize(base_klass, table, children)
|
||||
super(base_klass, children)
|
||||
@table = table
|
||||
end
|
||||
|
||||
def match?(other)
|
||||
return true if self == other
|
||||
super && base_klass == other.base_klass
|
||||
end
|
||||
|
||||
def table
|
||||
base_klass.arel_table
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -397,7 +397,7 @@ module ActiveRecord
|
|||
|
||||
def construct_join_dependency(joins = [], eager_loading: true)
|
||||
including = eager_load_values + includes_values
|
||||
ActiveRecord::Associations::JoinDependency.new(@klass, including, joins, eager_loading: eager_loading)
|
||||
ActiveRecord::Associations::JoinDependency.new(klass, table, including, joins, eager_loading: eager_loading)
|
||||
end
|
||||
|
||||
def construct_relation_for_association_calculations
|
||||
|
|
|
@ -119,9 +119,10 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
|
||||
join_dependency = ActiveRecord::Associations::JoinDependency.new(other.klass,
|
||||
joins_dependency,
|
||||
[])
|
||||
join_dependency = ActiveRecord::Associations::JoinDependency.new(
|
||||
other.klass, other.table, joins_dependency, []
|
||||
)
|
||||
|
||||
relation.joins! rest
|
||||
|
||||
@relation = relation.joins join_dependency
|
||||
|
|
|
@ -1020,9 +1020,7 @@ module ActiveRecord
|
|||
join_list = join_nodes + convert_join_strings_to_ast(manager, string_joins)
|
||||
|
||||
join_dependency = ActiveRecord::Associations::JoinDependency.new(
|
||||
@klass,
|
||||
association_joins,
|
||||
join_list
|
||||
klass, table, association_joins, join_list
|
||||
)
|
||||
|
||||
join_infos = join_dependency.join_constraints stashed_association_joins, join_type
|
||||
|
|
|
@ -1785,15 +1785,15 @@ class RelationTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
test "using a custom table affects the wheres" do
|
||||
table_alias = Post.arel_table.alias("omg_posts")
|
||||
|
||||
table_metadata = ActiveRecord::TableMetadata.new(Post, table_alias)
|
||||
predicate_builder = ActiveRecord::PredicateBuilder.new(table_metadata)
|
||||
relation = ActiveRecord::Relation.create(Post, table_alias, predicate_builder)
|
||||
|
||||
post = posts(:welcome)
|
||||
|
||||
assert_equal post, relation.where!(title: post.title).take
|
||||
assert_equal post, custom_post_relation.where!(title: post.title).take
|
||||
end
|
||||
|
||||
test "using a custom table with joins affects the joins" do
|
||||
post = posts(:welcome)
|
||||
|
||||
assert_equal post, custom_post_relation.joins(:author).where!(title: post.title).take
|
||||
end
|
||||
|
||||
test "#load" do
|
||||
|
@ -1950,4 +1950,13 @@ class RelationTest < ActiveRecord::TestCase
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def custom_post_relation
|
||||
table_alias = Post.arel_table.alias("omg_posts")
|
||||
table_metadata = ActiveRecord::TableMetadata.new(Post, table_alias)
|
||||
predicate_builder = ActiveRecord::PredicateBuilder.new(table_metadata)
|
||||
|
||||
ActiveRecord::Relation.create(Post, table_alias, predicate_builder)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue