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

  • Home
  • SEARCH
  • 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 8040575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:56:35+00:00 2026-06-05T03:56:35+00:00

The title is the problem in a nutshell. Here are the simplified models I

  • 0

The title is the problem in a nutshell. Here are the simplified models I am using :

class Test
  include Mongoid::Document

  field :name, :type => String
  embeds_many :questions
  attr_accessible :name, :questions      
end

class Question
  include Mongoid::Document

  field :text, :type => String

  embedded_in :test
  has_many :answers
  attr_accessible :text, :test, answers    
end

class Answer
  include Mongoid::Document

  field :value, :type => Integer

  belongs_to :question
  belongs_to :user
  attr_accessible :value, :question, :user      
end

class User
  include Mongoid::Document

  has_many :answers
  attr_accessible :answers    
end 

I want to be able to retrieve all the unanswered questions in a test for a user with the minimal amount of database queries.

This is the best solution I have thought up so far

answered = []
user.answers.each {|a| answered << a.question_id}
test.questions.where(:_id.nin => answered).length

Any help with this is much appreciated.

  • 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-05T03:56:37+00:00Added an answer on June 5, 2026 at 3:56 am

    UPDATE:
    you need to query for the non-existence of a constrained select:

    test.questions.find_by_sql("WHERE NOT EXISTS (SELECT id FROM answers WHERE answers.question_id = questions.id AND answers.user_id = #{user.id}") 
    

    or count 0 of a conditional join

    test.questions.joins("INNER JOIN answers ON answers.question_id = questions.id AND answers.user_id = #{user.id} AS user_answers").where('COUNT(user_answers.id) = 0')
    

    let me know if these work. any insight on their performance?

    ORIGINAL ANSWER, INCORRECT cos it was assuming we need totally unanswered questions ASKED by the user:

    use a counter cache on answers. This assumes an answers_count integer column on the questions db table.

    class Answer
      include Mongoid::Document
    
      field :value, :type => Integer
    
      belongs_to :question, :counter_cache => true
      belongs_to :user
      attr_accessible :value, :question, :user      
    end
    

    Given this, your query can be:

    test.questions.where(answers_count: 0, user_id: user)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've problem like in title. I'm using ruby with Sinatra framework and trying to
The title describes my problem. E.g. public class EntryDAOModule extends AbstractModule { @Override protected
I am encountering this problem [see title] when debugging a unit test in Visual
The problem behind my question in the title is this: jQuery( document ).ready( function()
Now I have a problem as the title. Here is my environment: OS:ubuntu 10.10
Title should make my problem well described.Here goes my code. <div id=adiv><text>Some text</text></div> <script
Title is a pretty good indicator of my problem. Is there security concerns using
The problem is mostly summed up in the title. I am using the custom
Hard to come up with a proper title for this problem. Anyway... I'm currently
excuse me in advance if this is not the right title for the problem

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.