Arel improvements

Remove sql code and replace with higher level [arel](https://github.com/rails/arel) abstractions.
Inspired by http://robots.thoughtbot.com/using-arel-to-compose-sql-queries