rails, acts_as_rateable, will_paginate and sorting

Today I added will_paginate into my application (damn, that was easy! :-)). Yesterday I added acts_as_rateable with a cool web2.0 css star rating system. Not that easy, but a cool feature.

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.

What do you think of this post?
  • Awesome (0)
  • Interesting (0)
  • Useful (0)
  • Boring (0)
  • Sucks (0)

3 Antworten auf „rails, acts_as_rateable, will_paginate and sorting“

Forget what I said here, it’s really inefficient… I just stumbled upon (or actually really searched for) a article, which describes an easy way to cache the rating and make searches as performant as it was before we put the rating into an extra table.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.