2017-05-09 72 views
0

在我的应用程序中,我有模型Visit & PostRuby on Rails - PostgreSQL在加入群组时出现分组错误

class Post < ActiveRecord::Base 
    has_many :visits 

class Visit < ActiveRecord::Base 
    belongs_to :post, :counter_cache => true 

我试着让所有visits一个postvisits表。我所做的:

- a = Visit.joins(:post).group(:post_id).select(:post_id, :title, 'count(visits.id) as total_views').where(user: current_user) 

- a.each do |a| 
    %tr 
    %td= a.title 
    %td= a.total_views 

这工作找到自己的发展ENV /本地主机(我想既然我用sqlite3,布提现在用PostgreSQL的在我生产,我得到这个错误:

PG::GroupingError: ERROR: column "posts.title" must appear in the GROUP BY clause or be used in an aggregate function 
LINE 1: ...ECT count(visits.id) as total_views, "visits"."post_id", "title", c... 

我在做什么错,怎么解决?

回答

0

如答案"posts.title" must appear in the GROUP BY。在group中加入:title以及:post_id

Visit.joins(:post).group([:post_id, :title]).select('sum(cpc_bid) as earnings', :post_id, :title, 'count(visits.id) as total_views').where(influencer: current_user)