2017-05-09 5 views
0

In meiner Anwendung habe ich Modelle Visit & Post.Ruby on Rails - PostgreSQL Gruppierungsfehler beim Erstellen von Joins & Gruppe

class Post < ActiveRecord::Base 
    has_many :visits 

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

Im Versuch, alle visits ein post hat in visits Tisch zu bekommen. Ich tat:

- 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 

Dies funktioniert in meiner Entwicklung env finden/localhost (ich glaube, da ich sqlite3 verwenden), buti PostgreSql in meiner Produktion verwendet, und ich habe diesen Fehler:

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... 

Was mache ich falsch und wie repariere ich es?

Antwort

0

Wie die Antwort sagt "posts.title" must appear in the GROUP BY. In group hinzufügen :title zusammen mit :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)