Replaced count_by_sql with AR query