Fix query issues with ActiveRecord 4 adapters