提交 199db8c8 编写于 作者: J Jon Leighton

Hook ThroughAssociationScope up to use the AliasTracker class

上级 3f2e2580
......@@ -20,6 +20,7 @@ def construct_scope
end
# Build SQL conditions from attributes, qualified by table name.
# TODO: Conditions on joins
def construct_conditions
reflection = @reflection.through_reflection_chain.last
conditions = construct_quoted_owner_attributes(reflection).map do |attr, value|
......@@ -134,24 +135,44 @@ def construct_through_joins
joins.join(" ")
end
# TODO: Use the same aliasing strategy (and code?) as JoinAssociation (as this is the
# documented behaviour)
def alias_tracker
@alias_tracker ||= AliasTracker.new
end
def table_aliases
@table_aliases ||= begin
tally = {}
@reflection.through_reflection_chain.inject({}) do |aliases, reflection|
if tally[reflection.table_name].nil?
tally[reflection.table_name] = 1
aliases[reflection] = reflection.quoted_table_name
table_alias = quote_table_name(alias_tracker.aliased_name_for(
reflection.table_name,
table_alias_for(reflection, reflection != @reflection)
))
if reflection.macro == :has_and_belongs_to_many
join_table_alias = quote_table_name(alias_tracker.aliased_name_for(
reflection.options[:join_table],
table_alias_for(reflection, true)
))
aliases[reflection] = [table_alias, join_table_alias]
else
tally[reflection.table_name] += 1
aliased_table_name = reflection.table_name + "_#{tally[reflection.table_name]}"
aliases[reflection] = reflection.klass.connection.quote_table_name(aliased_table_name)
aliases[reflection] = table_alias
end
aliases
end
end
end
def table_alias_for(reflection, join = false)
name = alias_tracker.pluralize(reflection.name)
name << "_#{@reflection.name}"
name << "_join" if join
name
end
def quote_table_name(table_name)
@reflection.klass.connection.quote_table_name(table_name)
end
# Construct attributes for associate pointing to owner.
def construct_owner_attributes(reflection)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册