Review: multiple named_scopes with duplicate :joins fixed

Hey guys,

We've been chaining lots of named-scopes for advanced searching, and we're getting killed by the table aliasing issues. Often, multiple named scopes have to join in the same model (User needs Profile a lot, for example). When you combine them... aliasing issues!

Can we get some help reviewing this patch, it's our first submission to core:

http://rails.lighthouseapp.com/projects/8994/tickets/1077-chaining-scopes-with-duplicate-joins-causes-alias-problem

Thanks!

-David Stevenson & Joseph Palermo PivotalLabs