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 7590941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:31:55+00:00 2026-05-30T20:31:55+00:00

I have three tables: Users Questions User_Questions 1.user questions has the columns user_id, question_id,

  • 0

I have three tables:

  1. Users
  2. Questions
  3. User_Questions

    1.user questions has the columns user_id, question_id, and answer

I want to find a random question that has not been answered and thus does not have a row in the user_questions table.

If every question has been answered, then return any random question.

I’m told this can be done with an OUTER JOIN, but I’m a SQL noob and I’m not sure how to do that in Rails.
This is what I have:

def next_question          
  q = Question.all - Question.joins(:user_questions).where
       (user_questions: { user_id: user_id })
  q = Question.all if q.empty?
  return q[rand(q.size)]
end  
  • 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-05-30T20:31:56+00:00Added an answer on May 30, 2026 at 8:31 pm

    There’s hardly ever a good reason for calling the all method on a model class. This loads every single record in the database of that type into memory, and unless you’re absolutely certain this is a small set of records, you could potentially hang your entire system. Even then it’s extremely bad form to load in everything and then cherry pick one thing and discard the rest. It’s like ordering one of every single item from Amazon, picking out the pen you want, and throwing the rest of the delivery in the trash.

    What you probably want is something where you randomly select one record that has not been assigned yet. That probably looks something like this:

    Question.where('id NOT IN (SELECT question_id FROM user_questions WHERE user_id=?)', user_id).order('RAND()').first
    

    The problem with a JOIN is that you’re going to find records that have matches in the user_questions table and not the inverse.

    This query presumes that the number of questions answered by a user is relatively small or that NOT IN could get dramatically more expensive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables specifying important columns below Users(Id, username) Groups(Id, groupname, creator) (creator
I have three tables which are defined as: class User(Base): __tablename__ = 'users' id
Basically I have two models: User and Godfather. The godfather table has three columns:
I have a table like this, with three fields: User | Question# | Answer
Basicall I have three Tables Users Roles and USerRoles with Many to many Relationship.
In my database I have three tables: Users: UserID (Auto Numbering), UserName, UserPassword and
I have three database tables: users emails invitations Emails are linked to users by
Say I have three tables, a table of users, a table of around 500
I have users table. There are three other tables: developers, managers, testers. All of
I have three tables: User: UserId (pk) FirstName Lastname Messages: MessageId (pk) Heading Body

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.