Kaminari的next_page,prev_page和total_count方法因聚合函数而中断

这是从Rails 3升级到Rails 5之后开始发生的(使用带有PostgreSQL的activemodel 5.2.4.1)我对此所做的唯一更改是将Arel.sql()添加到查询的原始部分。不知道为什么现在由于以下错误而中断:

activeRecord::StatementInvalid (PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "community"
LINE 7: ...hived" = $1 GROUP BY community.id) subquery WHERE "community...
                                                             ^
: SELECT COUNT(*) FROM (SELECT 1 FROM "community" LEFT OUTER JOIN ( SELECT people.community_id,COUNT(people.id) as count,SUM(people.amount) as sum
          FROM people
          WHERE people.status = 'processed'
          GROUP BY people.community_id )
      AS all_people ON all_people.community_id = community.id WHERE "community"."archived" = $1 GROUP BY community.id) subquery WHERE "community"."archived" = $2):

查询模型:

  scope :index_community,-> () do
    all.joins(Arel.sql("
      LEFT JOIN ( SELECT people.community_id,SUM(people.amount) as sum
          FROM people
          WHERE people.status = 'processed'
          GROUP BY people.community_id )
      AS all_people ON all_people.community_id = community.id
      ")).select(Arel.sql("
        community.*,SUM(all_people.count) as number_of_people
      ")).group('community.id')
  end

在控制器中:

per_page = 30
query = Community.index_community
@communities = query.page(params[:page]).per(per_page)
@next_page = query.page(params[:page]).per(per_page).next_page
@previous_page = query.page(params[:page]).per(per_page).prev_page
@total_count = @communities.total_count

在index.json.jbuilder中:

json.results @communities do |community|
  json.name community.name
  json.number_of_people community.number_of_people.to_i
  json.action do
    json.edit edit_admin_community_path(community)
    json.view admin_community_path(community)
    json.delete admin_community_path(community)
  end
end

json.next_page @next_page

json.previous_page @previous_page

json.total_page @total_count

我可以在next_page和prev_page上使用no_count()方法,但这对total_count没有帮助。

tianli12345 回答:Kaminari的next_page,prev_page和total_count方法因聚合函数而中断

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2629207.html

大家都在问