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.<https://github.com/rails/rails/issues/5588>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-talk+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-talk@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.