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?