# Can 'tasks' belong to 'user' and 'project' at the same time?

**URL:** <https://www.ruby-forum.com/t/can-tasks-belong-to-user-and-project-at-the-same-time/141007>\
**Category:** Rails\
**Created:** [June 20, 2008, 6:29pm UTC](https://www.ruby-forum.com/t/can-tasks-belong-to-user-and-project-at-the-same-time/141007 "2008-06-20T18:29:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/6de8d8/32.png) [@nik](https://www.ruby-forum.com/u/nik)\
**Post date:** [June 20, 2008, 6:29pm UTC](https://www.ruby-forum.com/t/can-tasks-belong-to-user-and-project-at-the-same-time/141007/1 "2008-06-20T18:29:41Z")

</div>

Hello All!

I have ‘project’ and ‘task’ model that is set to belongs\_to ‘user’,  
and ‘user’ has\_many ‘projects’ and ‘task’, and I have the config/  
routes.db using

map.resource :users, :has\_many [:projects, :tasks]

I can use all seven actions in the users controllers to manipulate  
projects, for instance,

[http://localhost:3000/users/1/projects](http://localhost:3000/users/1/projects)

renders the ProjectController’s index action with index.html.erb  
showing all projects of user 1.

Same goes on for tasks. If I go to

[http://localhost:3000/users/1/tasks](http://localhost:3000/users/1/tasks)

the browser shows up the index list of all tasks belonging to this  
user 1

Would you be kind enough to show me some hints to realizing a  
functionality that when I go to

[http://localhost:3000/users/1/projects/1/tasks](http://localhost:3000/users/1/projects/1/tasks)

to see all the tasks in project 1 of user 1.

Thank You Very Much!

---

<div class="post-metadata">

**Author:** ![nik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/6de8d8/32.png) [@nik](https://www.ruby-forum.com/u/nik)\
**Post date:** [June 20, 2008, 8:03pm UTC](https://www.ruby-forum.com/t/can-tasks-belong-to-user-and-project-at-the-same-time/141007/2 "2008-06-20T20:03:56Z")

</div>

> [http://localhost:3000/users/1/projects/1/tasks](http://localhost:3000/users/1/projects/1/tasks)

I think you are mis-interrupting the meaning of that URI. users/1/  
projects/1/tasks would mean “Collection of tasks for project with id  
== 1 under the user with id == 1.”

In other words “users/1/project” is the collection of projects for a  
user with id == 1. Then projects/1/tasks is the collection of tasks  
for a project with id == 1. So if projects/1 is not associated to  
users/1 then users/1/projects/1/tasks would return an empty collection  
because users/1/projects/1 would not be found.

This would mean your database schema would be:

User: has\_many :projects  
Project: has\_many :tasks

Project: belongs\_to :user

Task: belongs\_to :project

Which is not the structure of the schema you presented.

---

<div class="post-metadata">

**Author:** ![nik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/6de8d8/32.png) [@nik](https://www.ruby-forum.com/u/nik)\
**Post date:** [June 21, 2008, 2:00am UTC](https://www.ruby-forum.com/t/can-tasks-belong-to-user-and-project-at-the-same-time/141007/3 "2008-06-21T02:00:59Z")

</div>

Hi Robert,

Let me make sure I am getting this right:

[http://dns/users/1/projects/1/tasks](http://dns/users/1/projects/1/tasks)

returns the collection of tasks with the criteria requiring that these  
tasks belong to project 1 and these tasks belong to user 1

So if we put it in context, will these url find the following  
sentence:  
I want to see Nik’s Cleaning Garage tasks where Nik is the user,  
Cleaning Garage is HIS project, the tasks are the get the oil stains  
and buy more shelves and etc.

Thank you so much for your time.

Nik
