# How to create multiple objects of one model in one post request?

**URL:** <https://www.ruby-forum.com/t/how-to-create-multiple-objects-of-one-model-in-one-post-request/139291>\
**Category:** Rails\
**Created:** [June 2, 2008, 5:21pm UTC](https://www.ruby-forum.com/t/how-to-create-multiple-objects-of-one-model-in-one-post-request/139291 "2008-06-02T17:21:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [June 2, 2008, 5:21pm UTC](https://www.ruby-forum.com/t/how-to-create-multiple-objects-of-one-model-in-one-post-request/139291/1 "2008-06-02T17:21:42Z")

</div>

Hi,

I would apreciate to hear suggestions from you on how to create  
multiple objects (of the same model) with one form and post request.

My concrete situation:  
I have a googlemaps application where users can click on a map to add  
a point to both the map and the html form vía javascript. When all  
desired points are added the user should be able to save all points  
within one post request to the corresponding controller/action.  
Do I have to write a specific action which reads the request’s  
different point params or is this anyhow possible with the standard  
create action plus a certain modification?

Thanks for your contribution!  
Jochen

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [June 2, 2008, 9:17pm UTC](https://www.ruby-forum.com/t/how-to-create-multiple-objects-of-one-model-in-one-post-request/139291/2 "2008-06-02T21:17:38Z")

</div>

can you make an array or collection while user clicks on map and when  
user clicks DONE you can run a loop to get all the points to save?  
Ajit

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [June 2, 2008, 10:33pm UTC](https://www.ruby-forum.com/t/how-to-create-multiple-objects-of-one-model-in-one-post-request/139291/3 "2008-06-02T22:33:49Z")

</div>

Jochen,  
Building on Ajit’s solution, you can modify create action/method to act  
on  
array of points.  
ActiveRecord doesn’t have any method to create/save multiple objects.

However if you have

Map has\_many Points

Point belongs\_to Map

all you need to do is insert all those points in you map object and call  
a  
save on Map object.  
i.e  
@map = Map.new  
@map.points \<\< point1  
@map.points \<\< point2  
.  
.  
.  
@map.save

Hope this helps

Regards  
Pavan

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [June 3, 2008, 4:57pm UTC](https://www.ruby-forum.com/t/how-to-create-multiple-objects-of-one-model-in-one-post-request/139291/4 "2008-06-03T16:57:40Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Monday 02 June 2008 17:21:18 jawosis wrote:

> Do I have to write a specific action which reads the request’s  
> different point params or is this anyhow possible with the standard  
> create action plus a certain modification?

Yes, but it won’t have to work very hard.

The form element names should look like:

points[][x]  
points[][y]  
points[][title]

The trick is that the “points[]” prefix for form input names is  
understood by Rails to mean that you’re posting an array of things.

Then, in your controller, params[:points] will be an array of hashes,  
each of which looks like this:

{ :x =\> 54.1, :y =\> 27.2, :title =\> “Some place” }

So you can do

Point.create( params[:points] )

because ActiveRecord::base.create takes an hash _or_ an array.

Ciao,  
Sheldon.  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFIRVuhpGJX8XSgas0RAgZOAJ4xlcOF+u40mnEzdCNrIBU48vQRaQCeLS7H  
yk6GqO4+D04qBZWITPhl8YI=  
=J+Ff  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [June 3, 2008, 3:23am UTC](https://www.ruby-forum.com/t/how-to-create-multiple-objects-of-one-model-in-one-post-request/139291/5 "2008-06-03T03:23:17Z")

</div>

Jochen Kempf wrote:

> Hi,
> 
> I would apreciate to hear suggestions from you on how to create  
> multiple objects (of the same model) with one form and post request.
> 
> My concrete situation:  
> I have a googlemaps application where users can click on a map to add  
> a point to both the map and the html form vï¿½a javascript. When all  
> desired points are added the user should be able to save all points  
> within one post request to the corresponding controller/action.  
> Do I have to write a specific action which reads the request’s  
> different point params or is this anyhow possible with the standard  
> create action plus a certain modification?
> 
> Thanks for your contribution!  
> Jochen

I would think the easiest way to do this would be to create a JSON  
object when the page loads, update it with each click on the map and  
when the user submits the form, push that object through to your  
controller. You should be able to iterate through the JSON object from  
your params[] and create your points right off that using the method in  
the reply above mine.
