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

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,


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


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


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

Thank You Very Much!


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.

Hi Robert,

Let me make sure I am getting this right:


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
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.