sql - Order by count on multiple associations -


I have a post model I a post has several comments, some are favorite, and there is a column on the post model Which is called the scene. I want to order posts on the basis of all these 3 digits. How can I achieve it?

You want that anyone can use find_by_sql to write SQL.

sample code:

  post.find_b_sql ("posts.id, select posts.title, comments (separate comments. Id) as comments_cnt, count (count Different favorites .post_id = posts.id favorites Join favorites.post_id = posts.id by group posts.id, posts.title order by max (posts.views) + comments_cnt + favorites_cnt DESC ")  

Edit:

For the other dbms, only the above code work for sql_lite or db2:

  Post.find_by_sql ("posts.id's Select, posts.title, count (separate comments id) forms as comments_cnt Leave the comment left on comments as favorites_cnt from the counting (different favorites.id) posts. Post_id = posts.id left to join favorites on favorites. Post_id = posts.id by group posts.id Posts by poster order (posts.views) + count (specific comments id) + count (different avorites.id DESC ")  

Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -