Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8819917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:28:54+00:00 2026-06-14T05:28:54+00:00

I have a simple relationship Questionnaire has_many Answers I have multiple questionnaires. I want

  • 0

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.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:28:55+00:00Added an answer on June 14, 2026 at 5:28 am

    If you’re using Postgres I would do

    Answer.find(:all,
      :order => "answers.attr1 asc, answers.attr2 asc, answers.attr3 asc, ..." , :limit => 5,
      :select => "DISTINCT ON (answers.questionnaire_id) answers.*")
    

    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:

    Answer.order("answers.attr1 asc, answers.attr2 asc, answers.attr3 asc, ...").
      limit(5).select("DISTINCT ON (answers.questionnaire_id) answers.*")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple has_many/belongs_to relationship between Report and Chart. The issue I'm having
Alright so I have a simple has_many :through relationship in Rails 3.1 class Event
I have a simple has_many through relationship set up: class Tag < ActiveRecord::Base has_many
I have a simple relationship. A campaign is linked to two Pages. A page
I have a simple has_one/belongs_to relationship between two models. This is a new association
I have a simple one-to-many relationship. I would like to select rows from the
I have a fairly simple foreign key relationship displayed by an inline in Django's
I'm getting started with FubuMVC and I have a simple Customer -> Order relationship
I have a simple Book Author relationship class Author(models.Model): first_name = models.CharField(max_length=125) last_name =
I have a simple many to many relationship and I am wondering how you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.