我收到此错误:
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)
运行时:
@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)
以下是我Gemfile
的相关部分:
gem 'rails', '4.1.1'
gem 'will_paginate', '~> 3.0'
gem 'will_paginate-bootstrap'
我知道这个讨论https://github.com/Casecommons/pg_search/issues/167 以及此stackoverflow问题Rails 4.1.1 w/ pg_search - "PG::SyntaxError: ERROR: syntax error at or near "AS"" Bug
我相信我遇到了相同或相关的问题,但我没有使用.count
activerecord方法,而是编写一些自定义SQL来获取计数。所以,我不确定如何能够修复我的修复以解决我的问题。他们说,调用.count(:all)
而不仅仅是.count
解决了他们的问题。
如果您需要更多信息,请询问。
谢谢!
****更新**** 当我删除分页时,服务器不再崩溃。这是预期的,但我认为我仍然会添加更新。显然,我想修复错误,但仍然有分页。