# nested with scope problem

**URL:** <https://discuss.rubyonrails.org/t/nested-with-scope-problem/24695>\
**Category:** rubyonrails-talk\
**Created:** [February 11, 2008, 2:58am UTC](https://discuss.rubyonrails.org/t/nested-with-scope-problem/24695 "2008-02-11T02:58:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [February 11, 2008, 2:58am UTC](https://discuss.rubyonrails.org/t/nested-with-scope-problem/24695/1 "2008-02-11T02:58:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@Rick\_DeNatale](https://discuss.rubyonrails.org/u/Rick_DeNatale)\
**Post date:** [February 11, 2008, 2:08pm UTC](https://discuss.rubyonrails.org/t/nested-with-scope-problem/24695/2 "2008-02-11T14:08:35Z")

</div>

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 &nbsp;&nbsp;&nbsp;has\_and\_belongs\_to\_many :courses end

class Course \< ActiveRecord::Base &nbsp;&nbsp;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 &nbsp;&nbsp;&nbsp;&nbsp;has\_many :course\_enrollments &nbsp;&nbsp;&nbsp;&nbsp;has\_many :courses :through =\> :course\_enrollments end

class Course \< ActiveRecord::Base &nbsp;&nbsp;&nbsp;has\_many :course\_enrollments &nbsp;&nbsp;&nbsp;has\_many :students :through =\> :course\_enrollments end

class Course\_Enrollment \< ActiveRecord::Base &nbsp;&nbsp;&nbsp;belongs\_to :student &nbsp;&nbsp;&nbsp;belongs\_to :course end
