Rewrite using Arel.

Simplified things a bit and converted all queries to use Arel instead of concatenating SQL.  We won't be able to use this version until we upgrade Rails, but hopefully it will help with the scope merging issues we've had.