Skip to content

Commit

Permalink
Merge pull request #29828 from kamipo/fix_using_custom_table_with_joins
Browse files Browse the repository at this point in the history
Fix `JoinDependency` with using a custom table
  • Loading branch information
rafaelfranca committed Jul 17, 2017
2 parents f0b16af + ea09bf5 commit 589adea
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ def self.walk_tree(associations, hash)
# 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/finder_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ def construct_relation_for_exists(relation, conditions)

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
Expand Down
7 changes: 4 additions & 3 deletions activerecord/lib/active_record/relation/merger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@ def merge_joins
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
Expand Down
4 changes: 1 addition & 3 deletions activerecord/lib/active_record/relation/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1020,9 +1020,7 @@ def build_join_query(manager, buckets, join_type)
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
Expand Down
19 changes: 14 additions & 5 deletions activerecord/test/cases/relations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1785,15 +1785,15 @@ def test_presence
end

test "using a custom table affects the wheres" do
table_alias = Post.arel_table.alias("omg_posts")
post = posts(:welcome)

table_metadata = ActiveRecord::TableMetadata.new(Post, table_alias)
predicate_builder = ActiveRecord::PredicateBuilder.new(table_metadata)
relation = ActiveRecord::Relation.create(Post, table_alias, predicate_builder)
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, relation.where!(title: post.title).take
assert_equal post, custom_post_relation.joins(:author).where!(title: post.title).take
end

test "#load" do
Expand Down Expand Up @@ -1950,4 +1950,13 @@ def stubbed_connection.combine_bind_parameters(**kwargs)
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

0 comments on commit 589adea

Please sign in to comment.