Differenthink wrote:
I want to order by the last "reponse" datetime.
which should be : "responses.date desc"
but when doing that, it seems that it sorts by the first "reponse"
found for a question and note the last "reponse" posted...
is it a bit clearer ?
(think for it as a forum, where there is one "question" and could be
many "answers" (=reponses)
On 22 juin, 00:25, Phillip Koebbe <rails-mailing-l...@andreas-s.net>
Are you trying to display a list of questions according to which one has
the most recent response? The finder that you posted earlier
@question = Question.find(:all, :conditions=>["systeme_id IN (?)",
@systeme], :include=> :reponses, :order=>"reponses.date DESC", :page
=>{ :size=>10, :current=>params[:page]})
is looking for all questions for the given systeme_ids, and will join to
the responses table on each question. So your order clause is going to
include all the multiple responses for each question. I'm not sure
that's what you want. If you want to order the questions by the *most
recent response* for each question, you will need to create an
association such as
class ForumPost < ActiveRecord::Base
has_one :most_recent_response, :class_name => 'Response', :foreign_key
=> 'question_id', :order => 'date desc'
end
Then in the finder, include that association
Question.find(:all, :include => :most_recent_response, :order =>
'responses.date desc')
Something like that anyway. I'm using some code I wrote earlier this
year using ForumPosts as a guide, but in my case, the most recent reply
is also a ForumPost, so my association is a little more complicated. But
I did something similar.
Oh, now that I look more at my code, I see that I constructed custom SQL
to do the finding of replies, so this may not work like I think. Give
it a try, though, and see what happens.
Peace,
Phillip