2017-05-09 12 views
0

私のアプリケーションで私はモデルがありますVisit & PostRuby on Rails - 結合とグループを実行するときのPostgreSQLグループ化エラー

class Post < ActiveRecord::Base 
    has_many :visits 

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

イムpostvisitsテーブルに持っているすべてのvisitsを取得しようとしています。これは、私の開発のenv/localhostの(私はsqlite3を使用するので、私は思う)、ブーティで見つける働く

- 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 

私の生産に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にを加えて: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) 
関連する問題