Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku PostgreSQL GROUP_BY error in Rails app

I've got a rails app that works fine in development (SQLite) but is throwing lots of errors when I've deployed it via Heroku, which uses PostgreSQL I gather.

the error message getting returned:

        ActionView::Template::Error (PGError: ERROR:  
column "practices.id" must appear in the GROUP BY clause or be used in an aggregate function: 
SELECT "practices".* 
FROM "practices" WHERE ("practices".activity_id = 1) 
AND ("practices"."created_at" BETWEEN '2011-01-01' AND '2011-01-31') 
GROUP BY DATE(created_at) ORDER BY created_at DESC):

this is being thrown when I call the following:

def month_days_not_practiced(date = Date.today)
   p = practices.where(:created_at => date.at_beginning_of_month..date.at_end_of_month).group("DATE(created_at)").to_a.count
   days_in_month(date.year, date.month) - p
end

I'd really like to keep the code clean so it works on both development and production DBs...can anyone shed some light?

I have tried this:

def month_days_not_practiced(date = Date.today)
   p = practices.where(:created_at => date.at_beginning_of_month..date.at_end_of_month).group("practices.id, DATE(created_at)").to_a.count
   days_in_month(date.year, date.month) - p
end

to no avail...

tia.

like image 717
The Pied Pipes Avatar asked Jan 21 '23 19:01

The Pied Pipes


1 Answers

Practice.group(Practice.col_list)



def self.col_list
  Practice.column_names.collect {|c| "practices.#{c}"}.join(",")
end
like image 122
Paul Ketelle Avatar answered Jan 23 '23 08:01

Paul Ketelle