# Has\_many, through to has\_many

**URL:** <https://www.ruby-forum.com/t/has-many-through-to-has-many/122318>\
**Category:** Rails\
**Created:** [December 6, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/has-many-through-to-has-many/122318 "2007-12-06T16:28:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [December 6, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/has-many-through-to-has-many/122318/1 "2007-12-06T16:28:51Z")

</div>

I have a situation where I need to get Courses for a particular User.  
Courses are associated with Divisions that in turn are related to the  
user  
by a has\_many, through relationship. Ideally, I’d like to stay within  
the  
framework and do something like this:

> @courses = @user.divisions.courses

Or if I’m looking for a particular course I could do this:

> @course = @user.divisions.courses.find(Params[:id])

Is this completely outside of the framework?

Here is the model setup I have:

> end

I managed to get a list of all Courses by adding a function in the User  
model, but this isn’t ideal for a few reasons, the most important being  
if I  
only need to find one Course, I’m forced to retrieve all of the courses  
and  
then perform an @courses.select{yada yada}. There must be a Better  
Way™! Any ideas?

class User \< ActiveRecord::Base

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [December 7, 2007, 6:06am UTC](https://www.ruby-forum.com/t/has-many-through-to-has-many/122318/2 "2007-12-07T06:06:50Z")

</div>

I’m assuming you know the ID of the course you want to select.

Why don’t you just do

Course.find(params[:id])

instead of:

@course = @user.divisions.courses.find(Params[:id])

On Dec 7, 2007 1:58 AM, Joe C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > @course = @user.divisions.courses.find(Params[:id])
> > 
> > has\_many :courses  
> > only need to find one Course, I’m forced to retrieve all of the courses and  
> > self.divisions.each do |division|
> 
> >

–  
Ryan B.

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [December 7, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/has-many-through-to-has-many/122318/3 "2007-12-07T16:30:57Z")

</div>

Hey, thanks for the reply.

It’s a validation check to make sure that the url isn’t spoofed by the  
user. We don’t want one user editing a course that isn’t associated to  
them:  
[www.example.com/course/edit/6](http://www.example.com/course/edit/6)

Changed to  
[www.example.com/course/edit/16](http://www.example.com/course/edit/16)

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [December 7, 2007, 5:30pm UTC](https://www.ruby-forum.com/t/has-many-through-to-has-many/122318/4 "2007-12-07T17:30:23Z")

</div>

Joe C. wrote:

> Hey, thanks for the reply.
> 
> It’s a validation check to make sure that the url isn’t spoofed by the  
> user. We don’t want one user editing a course that isn’t associated to  
> them:  
> [www.example.com/course/edit/6](http://www.example.com/course/edit/6)
> 
> Changed to  
> [www.example.com/course/edit/16](http://www.example.com/course/edit/16)

fine, take all my fun away.
