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).
On Jun 18, 8:16 am, Differenthink
<guillaume.mont...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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..
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---