2017-05-09 74 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) 
相關問題