# Nested with scope problem

**URL:** <https://www.ruby-forum.com/t/nested-with-scope-problem/128734>\
**Category:** Rails\
**Created:** [February 11, 2008, 3:58am UTC](https://www.ruby-forum.com/t/nested-with-scope-problem/128734 "2008-02-11T03:58:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![veil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a9a28c/32.png) [@veil](https://www.ruby-forum.com/u/veil)\
**Post date:** [February 11, 2008, 3:58am UTC](https://www.ruby-forum.com/t/nested-with-scope-problem/128734/1 "2008-02-11T03: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:** ![veil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a9a28c/32.png) [@veil](https://www.ruby-forum.com/u/veil)\
**Post date:** [February 11, 2008, 3:09pm UTC](https://www.ruby-forum.com/t/nested-with-scope-problem/128734/2 "2008-02-11T15:09:41Z")

</div>

On 2/10/08, Veil Yw [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://talklikeaduck.denhaven2.com/)
