I receive this error:
Completed 500 Internal Server Error in 73ms
2014-06-24T16:23:01.334241+00:00 app[web.1]: Processing by CollectionsController#index as HTML
2014-06-24T16:23:01.409751+00:00 app[web.1]: PG::SyntaxError: ERROR: syntax error at or near "as"
2014-06-24T16:23:01.409757+00:00 app[web.1]: LINE 1: ...T(collections.*, count(favorites.favoriteable_id) as "collec...
2014-06-24T16:23:01.409760+00:00 app[web.1]: ^
2014-06-24T16:23:01.409763+00:00 app[web.1]: : SELECT COUNT(collections.*, count(favorites.favoriteable_id) as "collection_count") AS count_collections_all_count_favorites_favoriteable_id_as_collec, collections.id AS collections_id FROM "collections" INNER JOIN "favorites" ON "favorites"."favoriteable_id" = "collections"."id" AND "favorites"."favoriteable_type" = 'Collection' GROUP BY collections.id
2014-06-24T16:23:01.410758+00:00 app[web.1]: Rendered collections/index.html.erb within layouts/application (63.4ms)
When running this:
@collections = Collection.joins(:favorites).select('collections.*, count(favorites.favoriteable_id) as "collection_count"').group("collections.id").order('collection_count desc').paginate(:page => params[:page], :per_page => 12)
Here is the relevant parts of my Gemfile
:
gem 'rails', '4.1.1'
gem 'will_paginate', '~> 3.0'
gem 'will_paginate-bootstrap'
I'm aware of this discussion https://github.com/Casecommons/pg_search/issues/167 as well as this stackoverflow question Rails 4.1.1 w/ pg_search - "PG::SyntaxError: ERROR: syntax error at or near "AS"" Bug
I believe I am experiencing the same or related issue but I am not using the .count
activerecord method but rather writing some custom SQL to get a count. So, I'm not sure how I could mirror their fix to fix my situation. They say that calling .count(:all)
instead of just .count
fixed their problem.
If you need anymore information, please ask.
Thanks!
**** Update **** When I remove pagination the server no longer crashes. This is expected but thought I would still add the update. Obviously, I would like to fix the error but still have pagination.