# Locating nested resources - how do you do it?

**URL:** <https://www.ruby-forum.com/t/locating-nested-resources-how-do-you-do-it/109354>\
**Category:** Rails\
**Created:** [July 13, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/locating-nested-resources-how-do-you-do-it/109354 "2007-07-13T18:19:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [July 13, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/locating-nested-resources-how-do-you-do-it/109354/1 "2007-07-13T18:19:02Z")

</div>

I struggle with this all the time and I haven’t come up with a solution  
I’m  
happy with so I thought I’d ask the group for some opinions.

Let’s assume I have something like this in my routes file:

map.resources :users do |u|  
u.resources :projects do |p|  
p.resources :tasks  
end  
end

When you go to /users/1/projects

you might consider looking up the project like this:

@project = Project.find(params[:id])

or like this:

@proejct = current\_user.projects.find(params[:id])

because no other user should be able to see this users’ projects…

But what do you do when you start looking at tasks?  
/users/1/projects/1/tasks

Do you find the project through the user and then show the tasks?

What about a specific task? (/users/1/projects/1/tasks/1)

Do you just look up the task and then figure out if it belongs to a  
project  
the user owns? Do you store the user ID an any object the user account  
“owns” to make the lookups easier?

I’ve tried lots of these methods, but the one I’m happiest with is just  
leaving the finders alone and using before\_filters to figure out if you  
have  
access, but that sometimes results in more queries.

Just lookin’ for some of your thoughts. I hope I’m being clear enough on  
what I’m asking.

Thanks much!

-Brian H.

---

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [July 14, 2007, 9:43am UTC](https://www.ruby-forum.com/t/locating-nested-resources-how-do-you-do-it/109354/2 "2007-07-14T09:43:39Z")

</div>

Brian H. wrote:

> But what do you do when you start looking at tasks?  
> /users/1/projects/1/tasks
> 
> Do you find the project through the user and then show the tasks?

@user = User.find(params[:user\_id])  
@project = @user.projects.find(params[:project\_id])  
@tasks = @project.tasks

Pete Y.

> **[Pete Yandell — Principal Engineer / Architect](https://notahat.com/)**
>
> Pete Yandell is an experienced Principal Software Engineer and Architect based in Melbourne, Australia.
