Bob Sanders
2007-Aug-05 11:13 UTC
How would I find all "Exams" that belong to a "Student"?
I know this is wrong: @exams = Exam.find(:all, :conditions => ["class.student_id = ?", current_user.id]) How would I correct the above to make it work? I know I can''t have class.student_id, since that''s not a column name in the database table. Beyond that, I have no idea how to find all exams that belongs to a student. Any input? -- 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 -~----------~----~----~----~------~----~------~--~---
Conrad Taylor
2007-Aug-05 11:44 UTC
Re: How would I find all "Exams" that belong to a "Student"?
Hi, if you have the following models class Student < ActiveRecordBase has_and belongs_to_many :exams end class Exam < ActiveRecordBase has_and_belongs_to_many :students end # What exams did a student take student = Student.find( student_id ) exams_taken = student.exams For a better explaination of Rails relationships, I would highly recommend reading AWDwRv2 chapter 18. Good luck, -Conrad On 8/5/07, Bob Sanders <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > I know this is wrong: > > @exams = Exam.find(:all, :conditions => ["class.student_id = ?", > current_user.id]) > > How would I correct the above to make it work? > > I know I can''t have class.student_id, since that''s not a column name in > the database table. > > Beyond that, I have no idea how to find all exams that belongs to a > student. > > Any input? > -- > 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 -~----------~----~----~----~------~----~------~--~---
Zach Inglis // LT3media
2007-Aug-05 12:27 UTC
Re: How would I find all "Exams" that belong to a "Student"?
Leave class out of it, as the id is not relative to the class. So: user.rb model: has_many :exams belongs_to :class exam.rb model: belongs_to :user controller: user = User.find(current_user.id) user.exams #=> [1,Math], [2,English], etc. Cheers, Zach Inglis → Blog -- http://www.zachinglis.com → Company -- http://www.lt3media.com → Portfolio -- http://portfolio.zachinglis.com On Aug 5, 2007, at 7:13 PM, Bob Sanders wrote:> > I know this is wrong: > > @exams = Exam.find(:all, :conditions => ["class.student_id = ?", > current_user.id]) > > How would I correct the above to make it work? > > I know I can''t have class.student_id, since that''s not a column > name in > the database table. > > Beyond that, I have no idea how to find all exams that belongs to a > student. > > Any input? > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
U can do: student.exams assuming u have student has-many exams declaration in ur student model Sent from my iPhone On Aug 5, 2007, at 4:13 AM, Bob Sanders <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org > wrote:> > I know this is wrong: > > @exams = Exam.find(:all, :conditions => ["class.student_id = ?", > current_user.id]) > > How would I correct the above to make it work? > > I know I can''t have class.student_id, since that''s not a column name > in > the database table. > > Beyond that, I have no idea how to find all exams that belongs to a > student. > > Any input? > -- > 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 -~----------~----~----~----~------~----~------~--~---