Skip to content

Instantly share code, notes, and snippets.

@jdunphy
Created January 15, 2010 22:42
Show Gist options
  • Save jdunphy/278485 to your computer and use it in GitHub Desktop.
Save jdunphy/278485 to your computer and use it in GitHub Desktop.
def aliased_table_name_for(name, suffix = nil)
if !parent.table_joins.blank? && parent.table_joins.to_s.downcase =~ %r{join(\s+\w+)?\s+#{active_record.connection.quote_table_name name.downcase}\son}
@join_dependency.table_aliases[name] += 1
end
unless @join_dependency.table_aliases[name].zero?
# if the table name has been used, then use an alias
name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}#{suffix}"
table_index = @join_dependency.table_aliases[name]
@join_dependency.table_aliases[name] += 1
name = name[0..active_record.connection.table_alias_length-3] + "_#{table_index+1}" if table_index > 0
else
@join_dependency.table_aliases[name] += 1
end
name
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment