A patch for tricky joins causing table name collisions

In master, there's a regression in the new association handling cod. Way back in a patch I submitted to 3.0 to fix similar problems with eager loading, there was another edge case that got "accidentally" fixed as well.

The issue is this:

In JoinAssociation#aliased_table_name_for, the following code is run to generate an aliased table name:

name = connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}#{suffix}" table_index = aliases[name] + 1 name = name[0, connection.table_alias_length-3] + "_#{table_index}" if table_index > 1

After this, aliases[name] is incremented. But since the value of "name" is now the new, aliased table name, the effect is that we end up with an aliases hash that looks something like this (taken from a multiply self-referential parent-child join:

{   "people"=>1, "children_people"=>1,   "children_people_2"=>2, "parents_people"=>1,   "parents_people_2"=>2 }

As you can see, instead of counting aliases against children_people, we began counting against the aliased table name itself. This leads the JoinDependency to create invalid JoinAssociations (2 against children_people_2 and parents_people_2 in this case, which means twice it incremented children_people and landed on the same table name), and creates table name collisions.

The patch at #6423 [PATCH] fix table name collision on certain tricky association joins - Ruby on Rails - rails fixes the regression and adds a test to cover it in the future.

Pushed to master, thanks.