# Help using a single form to update three models

**URL:** <https://www.ruby-forum.com/t/help-using-a-single-form-to-update-three-models/131505>\
**Category:** Rails\
**Created:** [March 10, 2008, 7:54pm UTC](https://www.ruby-forum.com/t/help-using-a-single-form-to-update-three-models/131505 "2008-03-10T19:54:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mikec](https://www.ruby-forum.com/u/mikec)\
**Post date:** [March 10, 2008, 7:54pm UTC](https://www.ruby-forum.com/t/help-using-a-single-form-to-update-three-models/131505/1 "2008-03-10T19:54:58Z")

</div>

Really hoping someone on this forum can help me out - any help is  
greatly appreciated.

I have been through the Ryan B railcasts and tutorials on using a  
single form to update more than one model. They are all based on the  
concept of having Project model that has many Tasks. The application  
I am working on requires a third level of model added to the form so  
Projec has many Tasks and Task has many Task Details. I can display  
the form in the “new” action so that all fields show up and it appears  
right. However, I can not get the “create” to work, not sure if the  
problem is “create” in my controller, or the hash being created in my  
fields\_for tag.

class Project \< ActiveRecord::Base  
has\_many :tasks

def task\_attributes=(task\_attributes)  
task\_attributes.each do |attributes|  
tasks.build(attributes)  
end  
end  
end

class Task \< ActiveRecord::Base  
has\_many :task\_details  
belongs\_to :projects

def task\_detail\_attributes=(task\_detail\_attributes)  
task\_detail\_attributes.each do |attributes|  
task\_details.build(attributes)  
end  
end

end

class TaskDetail \< ActiveRecord::Base  
belongs\_to :tasks  
end

class ProjectController \< ApplicationController

def index  
@projects = Project.find(:all)  
end

def new  
@project = Project.new  
@project.tasks.build  
@task = Task.new  
@task.task\_details.build  
end

def create  
@project = Project.new(params[:project])  
@task = Task.new(params[:task])  
params[:task].each\_value { |task\_detail|  
@task.task\_details.build{task\_detail} }  
if @project.save and @task.save  
flash[:notice] = “Successfully created project.”  
render :action =\> ‘index’  
else  
render :action =\> ‘new’  
end  
end

end

\<% form\_for :project, :url =\> { :action =\> :create } do |f| %\>

Name: \<%= f.text\_field :name %\> Date: \<%= f.date\_select(:date) %\>

 \<% for task in @project.tasks %\> \<% fields\_for "project[task\_attributes][]", task do |task\_form| %\> 

Name: \<%= task\_form.text\_field :name %\> Description: \<%= task\_form.text\_area :description %\> Fixed Task: \<%= task\_form.check\_box :fixed\_flg %\>

 \<% end %\>

\<% for task\_detail in @project.tasks %\>  
\<% fields\_for “task[task\_detail\_attributes][]”, task\_detail do |  
detail\_form| %\>

Sub Task: \<%= detail\_form.text\_field :sub\_task %\>  
Assigned: \<%= detail\_form.check\_box :assigned\_flg %\>

  
\<% end %\>  
\<% end %\>  
\<% end %\> 

\<%= submit\_tag "Create Project" %\>

\<% end %\>
