I have a simple relationship
Questionnaire has_many Answers
I have multiple questionnaires. I want to get the latest 5 answers across distinct questionnaires.
If I do:
Answer.find(:all, :order => "id desc" , :limit => 5)
I get the last 5 answers but most of the time all 5 answers belong to the same questionnaire. How can I query the latest answers from distinct questionnaires something like
Answer.find(:all, :order => "id desc" , :limit => 5, :conditions => "DISTINCT questionnaire.id") ??
(the idea is to show an activity feed to an administrator e.g. user A replied to answers in questionnaire X on 11-11-2012, user B replied to answers in questionnaire Y on 01-11-2012 etc.)
If you’re using Postgres I would do
Note you might have to list every column in your answer table except questionnaire_id in the order clause, so Postgres can know for sure what you want to select, otherwise it might be ambigious and you might get a SQL error.
And for gods sake upgrade to Rails 3!
Rails 3: