什麼是以下等效的鋼軌聲明?ruby on rails:什麼是find_by_sql的等價語句?
@temp = Post.find_by_sql("SELECT posts.id,posts.title, comment_count.count FROM posts INNER JOIN (SELECT post_id, COUNT(*) AS count FROM comments GROUP BY post_id) AS comment_count ON comment_count.post_id = posts.id ORDER BY count DESC LIMIT 5;")
是否可以將它轉換爲find/where/select語句?
這是一個複雜的查詢,我不能得到它,但嘗試過這樣的事情,
@temp = Post.select("posts.id, posts.title, comment_count.count").joins(:comments).group("post_id").order("countpages.counts desc").limit(5)
你要問每個查詢你有這個問題?請先嚐試弄清楚自己,然後如果你無法弄清楚,告訴我們你嘗試了什麼,有人會幫助你。 – Mischa
這是一個複雜的查詢,我無法得到它,但嘗試了這樣的事情,@temp = Post.select(「posts.id,posts.title,comment_count.count」)。joins(:comments).group(「 (5) – shibly
這不是一個壞問題... +1 :) – Nayish