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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:35:16+00:00 2026-06-15T07:35:16+00:00

I have these model: class Question has_many :answers end class Answer belongs_to :question end

  • 0

I have these model:

class Question
  has_many :answers
end

class Answer
  belongs_to :question
end

class Exam
  belongs_to :general_exam
  belongs_to :user
  has_many :questions, through: :exam_questions
end

class ExamQuestion
  belongs_to :exam
  belongs_to :question
end

Currently, I want to get all questions in Exam and answers of questions, so I used Specifying Conditions on Eager Loaded Associations, I ran this in console:

exam = Exam.find(16)
questions = Question.includes(:answers).where("id = ?", exam.question_ids)

Output in console after run questions = ...:

SELECT "questions".id FROM "questions" INNER JOIN "exam_questions" ON "questions"."id" = "exam_questions"."question_id" WHERE "exam_questions"."exam_id" = 16 ORDER BY questions.created_at DESC
  Question Load (0.8ms)  SELECT "questions".* FROM "questions" WHERE (id = 170,162,160) ORDER BY questions.created_at DESC
=> #<ActiveRecord::Relation:0x4c07ebc>

The first strange thing is, I saw in query, it did a INNER JOIN, but in rails guide, it said query will make a LEFT OUTER JOIN, I don’t know why this is different.

Second thing, now I want to get question objects in questions, I ran:

questions.each do |q|
  puts q.content
end

It returned error:

SELECT "questions".* FROM "questions" WHERE (id = 170,162,160) ORDER BY questions.created_at DESC
ActiveRecord::StatementInvalid: PG::Error: ERROR:  argument of WHERE must be type boolean, not type record
LINE 1: SELECT "questions".* FROM "questions"  WHERE (id = 170,162,1...

How can I get question objects now?

  • 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-15T07:35:17+00:00Added an answer on June 15, 2026 at 7:35 am

    It looks like your where clause is wrong. Try:

    where(:id => exam.question_ids)
    

    When you provide the string version, “id = ?”, the database adapters won’t convert that to an in-clause. When you provide the hash version, the database adapters will recognize that the value is an array and use an in.

    Perhaps a more efficient way would be to approach the problem differently:

    class Question
      has_many :exam_questions
    end
    
    questions = Question.joins(:exam_questions).where(:exam_questions => {:exam_id => 16})
    

    See Efficient ActiveRecord has_and_belongs_to_many query

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

Sidebar

Related Questions

I have these models: class UserProfile(models.Model): user = models.OneToOneField(User) #etc class Organization(models.Model): users =
I defined these 3 models in Rails3. class User < ActiveRecord::Base has_many :questions has_many
I have these models: class Model1(models.Model): ... class Model2(models.Model): m1 = models.OneToOneField(Model1, related_name='m2') ...
I have these models: class Gallery(models.Model): HeadImage = models.ImageField(upload_to=gallery,blank=True,null=True) class News(Gallery): Name=models.CharField(max_length=100) #some other
I have these simple classes Class A(models.Model): ... Class Meta(models.Model): a = models.OnetoOneField(A, primary_key=True)
Let's say I have these models: class A(models.Model): name = models.CharField(max_length=32) b = models.ForeignKey(B,
I have 3 model, User, Post, Comment with definition like below class Post <
i have these two models public class Group { [Key] public int GroupID {
I have these view models. public class MasterFrmVm { public Guid Id { get;
Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a

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.