And then I wanted to combine those two. I couldn’t find any nice ways of doing this, but until someone tells me a better way, the following is working out for me and helps perhaps someone else out there. Even if it could be a bit nicer. 🙂
@quotes = Quote.paginate_by_sql "SELECT quotes.*, avg(ratings.rating) AS rating, count(ratings.rating) AS votes FROM `quotes` LEFT JOIN ratings ON quotes.id = ratings.rateable_id WHERE (ratings.rateable_type = 'Quote' OR ratings.rateable_type IS NULL) GROUP BY ratings.rateable_id ORDER BY rating DESC, votes DESC", :page => params[:page]
Edit: I fixed it, it returns now quotes without a rating too.