Fix count method to work with select statement.

`count` method raises `ActiveRecord::StatementInvalid` if `select` method is used in Rails 4.1. This commit fixes will paginate and if work if `select` method is used.
this looks very much like the code removed in https://github.com/rails/rails/pull/10710, which I suppose is as good a solution as any.