On 2/10/08, Veil Yw [email protected] wrote:
so, i think… table timetable must retrieve student’s class1_id from
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/