# Understanding Associations with Multiple forms on a Page

**URL:** <https://www.ruby-forum.com/t/understanding-associations-with-multiple-forms-on-a-page/226049>\
**Category:** Rails\
**Created:** [September 9, 2009, 9:42am UTC](https://www.ruby-forum.com/t/understanding-associations-with-multiple-forms-on-a-page/226049 "2009-09-09T09:42:26Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluehandtalking](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c4cdca/32.png) [@bluehandtalking](https://www.ruby-forum.com/u/bluehandtalking)\
**Post date:** [September 9, 2009, 9:42am UTC](https://www.ruby-forum.com/t/understanding-associations-with-multiple-forms-on-a-page/226049/1 "2009-09-09T09:42:26Z")

</div>

I have a project with the following relationships:

class Project \< ActiveRecord::Base  
has\_one :photo  
has\_one :project\_detail

class Photo \< ActiveRecord::Base  
belongs\_to :project, :accessible =\> true, :creator =\> true

class ProjectDetail \< ActiveRecord::Base  
belongs\_to :project, :accessible =\> true, :creator =\> true

I am trying to create a page with 4 forms, 3 to upload photos with  
paperclip, and one to upload project\_details.

The page is created by the ProjectsController. In that I currently  
have  
been creating the following in ‘new’:

```
@project = Project.new
@project.build_photo
@project.photo.save (If I save project, does it also save

```

project.photo?)  
@project.photo\_id = @project.photo.id (does rails do this  
automatically?)  
@project.build\_project\_detail  
@project.project\_detail.save

```
@project.project_detail_id = @project.project_detail.id (does

```

rails do this automatically?)  
@project.save  
redirect\_to :action =\> “edit”, :id =\> @project.id, :photo\_id =\>  
@project.photo\_id, :project\_detail\_id =\> @project.project\_detail\_id

The above seems very redundant and unnecessary. I would like to know a  
much better way of going about  
this. The reason for saving and redirecting to edit was so that the  
project id would be created when I upload  
a photo.

The page is supposed to function by being able to upload the 3 images,  
and then upload the project\_details.  
Since AJAX does not support file uploads, I am going to try and  
implement an ‘Ajaxy’ solution using iframes and the  
“responds\_to\_parent” plugin. However, first I would like to know  
answers to the following:

1. What is a better approach than the one above for setting up the new  
project page?

2. In my photo form I am passing @project.photo. This does get handled  
by the photo controller, but the  
following does not work in the PhotosController:

The above does not work.

So what is the correct way to achieve the above?

This does work: @photo = Photo.new(params[:photo]) …but is not  
what I want.  
The photo model has fields for 3 photos, so I need to use the same  
record for all  
3 uploads.

1. In the photos controller, what is the correct way to redirect to  
the page I just came from.

Thanks,

```
Jet

```

Oh, and a bonus question. If I have the above mentioned relationship  
with project and photo,  
and I create an instance in the projects controller: @project.photo

how would I access the photo data in the returned params hash from a  
form,  
e.g. to access the project data (params[:project])

would accessing photo part be (params[:project =\> :photo])

…and an individual element (params[:project =\> :photo  
=\> :name]) ???
