Kind of a complex find :order

Hi,

i ve got two models,
“Question” and “Answer”
A “question” can have many “answers”, or none.

I want to order my questions by answers date or question date if it
has no answer,
i tried to do that :

Question.find(:all, :order=>“answers.date DESC, questions.date DESC”)

But questions without answers appear always after questions with
answers, even when the question was created after an answer…

Could you give me an hints ?

Thanks a lot…

I think for this I would find all the questions (and eager load the
answers for those questions) and then sort the array. e.g.

class Question < ActiveRecord::Base
has_many :answers, :order => “answers.date DESC”

def latest_date_including_answers
if self.answers.blank?
return self.date
else
return answers.first.date
end
end

def self.newest_including_answers
find(:all, :include => :answers).sort{|a,b|
a.latest_date_including_answers <=>
b.latest_date_including_answers
}
end
end

The latest_date_including_answers method assumes that answers always
come after the question is asked (since it doesnt compare answer.date
to question.date).