Re: [Rails] Using :group with #count generating bad SQL in Postgres

2013-07-24 Thread Carlos Figueiredo
@Yaw, can you test if you change the way you call the count on postgres, rails still change the name of the column? Ex. Try using count(comments.id) or count(1) besides count(*) on your query and let me know what happens... Ps.: The ugly fix of my last email still works Em 24/07/2013 08:10, "Carl

Re: [Rails] Using :group with #count generating bad SQL in Postgres

2013-07-24 Thread Carlos Figueiredo
Now I figured out what is the error... I dont know why... But rails changed the column name where you count froum comments_count to count_all... And you refer it on your order by statement using the alias you choose... I hope somone more experienced on rails help you fix it, but for now... U could

Re: [Rails] Using :group with #count generating bad SQL in Postgres

2013-07-24 Thread Yaw Boakye elGran
Error message: PG::Error: ERROR: column "comments_count" does not exist LINE 1: ...l, titles.created_at, titles.updated_at ORDER BY comments_c... ^ : SELECT COUNT(*) AS count_all, titles.id, titles.title, titles.submitter_name, titles.

Re: [Rails] Using :group with #count generating bad SQL in Postgres

2013-07-24 Thread Yaw Boakye elGran
I think so, sorry for the misinformation. Thanks for the correction :) On Wed, Jul 24, 2013 at 12:36 AM, Scott Ribe wrote: > On Jul 23, 2013, at 6:28 PM, Yaw Boakye elGran > wrote: > > > • During grouping, the normal SQL requirement is to group on a > column that is on both tables. As f

Re: [Rails] Using :group with #count generating bad SQL in Postgres

2013-07-23 Thread Scott Ribe
On Jul 23, 2013, at 6:28 PM, Yaw Boakye elGran wrote: > • During grouping, the normal SQL requirement is to group on a column > that is on both tables. As far as I know, grouping can't be done on two > tables when they don't a common column. In your group call, only titles.id > would be

Re: [Rails] Using :group with #count generating bad SQL in Postgres

2013-07-23 Thread Yaw Boakye elGran
There are at least 2 problems with how you recommend the query to be written: 1. Why would titles.* work in regular SQL but not in Rails' selectmethod? What would be the case if I have 100 columns on the titles table? 2. During grouping, the normal SQL requirement is to group on a column

Re: [Rails] Using :group with #count generating bad SQL in Postgres

2013-07-23 Thread Carlos Figueiredo
Sorry... Are there more columns on titles entity? If yes... the problem is that you tried to show columns you weren't grouping... and on postgresql it blows an exception... but if there is only one column on titles... (titles.id) so, I can't figure out what's the problem you mentioned... What were

Re: [Rails] Using :group with #count generating bad SQL in Postgres

2013-07-23 Thread Carlos Figueiredo
def self.order_by_number_of_comments_descending select('titles.id, titles.name, titles.whatever, count(comments.id) AS comments_count'). joins(:comments). group('titles.id, titles.name, titles.whatever'). order('comment_count DESC') end Assuming titles model is like (id, name, wh

Re: [Rails] Using :group with #count generating bad SQL in Postgres

2013-07-23 Thread Yaw Boakye elGran
Can you write the right (no pun intended) query here? Thanks On Tue, Jul 23, 2013 at 12:36 PM, Carlos Figueiredo < carlos.figueired...@gmail.com> wrote: > Your query is wrong... > You need group all columns you show that arent the result of an aggregate > function... > So your group would be gro

Re: [Rails] Using :group with #count generating bad SQL in Postgres

2013-07-23 Thread Carlos Figueiredo
Your query is wrong... You need group all columns you show that arent the result of an aggregate function... So your group would be group('titles.*') Em 22/07/2013 18:22, "yaw" escreveu: > Rails 4.0.0, Ruby 2.0 > > class Title < ... > ... > > has_many :comments > > def self.order_by_number_

[Rails] Using :group with #count generating bad SQL in Postgres

2013-07-22 Thread yaw
Rails 4.0.0, Ruby 2.0 class Title < ... ... has_many :comments def self.order_by_number_of_comments_descending select('titles.*, count(comments.id) AS comments_count'). joins(:comments). group('titles.id'). order('comment_count DESC') end results in a malformed SQL on Po