Hi all. Let's say that quizzes have many tags, through taggings.
I'm doing a search on quizzes, including various other associations, and
passing in various conditions, such as "quizzes.name like '%english%'"
or "quizzes.subject = 'Language'".
If i'm given a parameter :tag => "banana" then i want to order my
results so that the quizzes are ordered by their tags, but with the ones
with the 'banana' tag pushed up to the top of the results. I'm doing a
paginated search (with will_paginate) so i don't want to get all of the
results with a regular find(:all) and then re-order them - i want to do
it in the sql
Can i do this by passing something to the 'order' option? Something
along these pseudo-code lines (the order option is not valid but
hopefully gets my requirement across):
The desc is necessary because "tags.name = 'banana'" comes out as 1 or 0
and we want the 1s to be shown before the 0s. But you know that as
that's why you set 'A' or 'Z' as the result of the test.
I like your solution though, it's more obvious to the reader what is
going on. Although i also like mine as it only involves changing the
'order' option and nothing else. Ultimately i guess we're doing the
same thing though.
Amendment - the last thing i posted didn't quite work - the rest of them
(ie which didn't have banana) weren't sorted as i had the logic a bit
screwed up. This is better: instead of this:
:order => "(tags.name = 'banana' or tags.name) desc"