How to use ActiveRecord associations to select from related.

table "users" [Stores user information] id, username, password,email

table "exams" [Stores exam information] id, name, description

table "questions" [stores question information] id, question, opt1, opt2, opt3,opt4, correct

table "exam_questions" [stores selection of questions for a particular exam or test] id, exam_id, question_id

table "user_answers" [stores the user response to the questions in exam] id, user_id,exam_id,question_id,userans, created_at

Associations in my models:

"user.rb"   has_many :user_answers

"exam.rb"   has_many :exam_questions   has_many :questions, :through=>:exam_questions   has_many :user_answers

"question.rb"   has_many :exam_questions   has_many :exams, :through=>:exam_questions   has_many :user_answers

"exam_question.rb"   belongs_to :exam   belongs_to :question

"user_answer.rb"   belongs_to :user   belongs_to :question   belongs_to :exam

Now what I need is: 1. To extract the exam and question details by running a query in ExamQuestion table. Is this possible?    something like: @detail=ExamQuestion.find(:all) and then looping also displaying the question and exam detail. I don't want to use find_by_sql

2. Also to extract form UserAnswer table and display the exam detail, question detail and user answer with a single hit in database.

In general terms, I want to use the features of Active Record associations to run a query to extract as much informaton as possible from multiple tables. This is to avoid multiple queries. Please help.

Please help me by responding to the above post.

Your help will be appreciated.

Thanks