# Pattern for creating child objects

**URL:** <https://www.ruby-forum.com/t/pattern-for-creating-child-objects/142044>\
**Category:** Rails\
**Created:** [July 2, 2008, 9:06pm UTC](https://www.ruby-forum.com/t/pattern-for-creating-child-objects/142044 "2008-07-02T21:06:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [July 2, 2008, 9:06pm UTC](https://www.ruby-forum.com/t/pattern-for-creating-child-objects/142044/1 "2008-07-02T21:06:55Z")

</div>

Hi, complete newbie question but I’d like to understand the  
recommended controller pattern for creating child objects of a parent.

The starting point is a display action for the parent: def show  
I have a button on the page which calls an action on the parent  
controller: def new\_child  
I have an action on the child controller for creating a new child with  
the parent passed in as an argument: def new (parent)

I’m struggling with how to pass the parent object through to the  
new\_child action from the parent controller. This makes me think that  
perhaps the pattern is wrong.

I would really welcome any thoughts, guidelines, etc.

Thanks,

Adam

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [July 2, 2008, 9:42pm UTC](https://www.ruby-forum.com/t/pattern-for-creating-child-objects/142044/2 "2008-07-02T21:42:49Z")

</div>

I don’t know enough about your data to know if this is a tree structure  
(self-referential join) or a parent-child in terms of 'a project has a  
task  
associated"

Either way the concept is the same. It’s trivial if you use RESTful  
routing.

## config/routes.rb

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

app/models/project.rb

has\_many :tasks

app/views/projects/index.html.erb

\<%=link\_to “Create new project”, new\_project\_task\_url(@project) %\>

/app/controllers/task\_controller.rb

before\_filter :find\_project

def index  
@tasks = @project.tasks.find :all  
end

def new  
@task = @project.tasks.build  
end

def create  
@task = @project.tasks.build(params[:task])  
if @task.save  
flash[:notice] = “Task added successfully!”  
redirect\_to project\_tasks\_url(@project)  
else  
render :action=\>“new”  
end  
end

private

```
  def find_project
    begin
       @project = Project.find(params[:project_id]) # the url sends 

```

the  
project in as params[:project\_id]  
rescue  
flash[:notice] = “Project not found”  
redirect\_to root\_url  
end  
end

The key is to make sure to pass the id of the project around in every  
URL  
you use and scope everything using the associations. Notice when I find  
tasks I do @project.tasks.find(:all) instead of Task.find(:all). This  
is a  
great way to ensure that I only get the things I need.

@task = @project.tasks.build is the same as @task =  
Task.new(:project\_id =\>  
some\_id).

If this is not clear enough, let me know how I can help.
