Bhai Jan
2008-Oct-02 00:22 UTC
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Please help me by responding to the above post. Your help will be appreciated. Thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---