On 2/10/08, Veil Yw
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:>
> hello there,
> im a beginner in ruby on rails.
>
> i have 3 tables:
> timetable --contains--> id, class1_id, subject_id, etc
> student --contains--> id, class1_id, etc
> class1 --contains--> id, classname
>
> now, i wanted to view student''s timetable..
> so, i think.. table timetable must retrieve student''s class1_id
from
> table student.
> please help me to settle this.
>
> i tried something like this but it cannot be run.. im lack of idea
> already:
> def timetable
> @stud_ttable = Student.find(:first, :conditions => ["class1_id =
?",
> student.class1_id])
> @timetables = Timetable.find(:all, :conditions => ["class1_id =
?",
> @stud_ttable.id])
> end
I think you''d be better off with using either has_many, perhaps with
the :through option.
students table has id and attributes about the student, NO classx ids.
courses table (I''m assuming you were trying to model the first class
in the student''s schedule with the name class1, don''t do that,
I chose
courses so that the model class name doesn''t conflict with the Ruby
Class class). Has id and attributes about the course..
Now two options. 1st and simplest.
courses_students table has class_id and student_id fields
class Student < ActiveRecord::Base
has_and_belongs_to_many :courses
end
class Course < ActiveRecord::Base
has_and_belongs_to many: students
end
Or if you want to have attributes associated with a particular
enrollment of a student in a course.
instead of the courses_student table have, say, a course_enrollment table with
id, student_id, course_id and any other attributes needed for the enrollment.
Now
class Student < ActiveRecord::Base
has_many :course_enrollments
has_many :courses :through => :course_enrollments
end
class Course < ActiveRecord::Base
has_many :course_enrollments
has_many :students :through => :course_enrollments
end
class Course_Enrollment < ActiveRecord::Base
belongs_to :student
belongs_to :course
end
--
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.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
-~----------~----~----~----~------~----~------~--~---