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.*')
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 Postgres,
SELECT COUNT(*) AS count_all, titles.id AS titles_id FROM "titles"
INNER JOIN "comments" ON "comments"."title_id" = "titles"."id" GROUP BY titles.id ORDER BY comments_count DESC
I've contacted @steveklabnik and he'd re-open an issue reporting this bug. In the meantime, has anybody got around this? The SQL is all correct until you call group.
Thanks for helping
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5def0eff-649b-4e00-9b87-154f02b9f34d%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CANPOtXsquwiLear7OOO3ZJ-r-wzoO499L8JAhOVCWLNQGLXyPQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAKwoQDvcJkAuDiQHV9sH6%3DiaXdBz%3DAEZzxqrW0odBJi33Oyb%3Dw%40mail.gmail.com.
Did you try running the resulting query? It should throw an exception concerning the GROUP BY
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CANPOtXu-qYKESOxwvgTjM4%2BVxgfFr06x7hEFMHG5t9P8ub_n1A%40mail.gmail.com.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4F79E8C6-3BB4-4FE0-A64C-7B55C92DF4E8%40elevated-dev.com.
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.submitter_email, titles.created_at, titles.updated_at AS titles_id_titles_title_titles_submitter_name_titles_submitter_e FROM "titles" INNER JOIN "comments" ON "comments"."title_id" = "titles"."id" GROUP BY titles.id, titles.title, titles.submitter_name, titles.submitter_email, titles.created_at, titles.updated_at ORDER BY comments_count DESC
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 do 2 fix...
You can call the alias rails choose (count_all) on your order statement (ugly way to solve)
Or you can call for count(*) without alias on your order statement (more practiced by dba... Less ugly to solve)
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAKwoQDu_wG%2BD3QsVQv0YoRMLdJwhT29kMT0znaFTp0vfrj0aXQ%40mail.gmail.com.
@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