use arel rather than slapping together SQL strings
Merge pull request #11880 from senny/backport_arel_fix

Backport arel fix