# Saving mutliple objects to database

**URL:** <https://www.ruby-forum.com/t/saving-mutliple-objects-to-database/165144>\
**Category:** Rails\
**Created:** [April 9, 2009, 2:55pm UTC](https://www.ruby-forum.com/t/saving-mutliple-objects-to-database/165144 "2009-04-09T14:55:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [April 9, 2009, 2:55pm UTC](https://www.ruby-forum.com/t/saving-mutliple-objects-to-database/165144/1 "2009-04-09T14:55:57Z")

</div>

If i create in memory a hundred or so instances of a model, how do i go  
about saving them to the database.

I have a Task Model

and say I have an array of Task obects

@tasks = […]

how do i save them

do i

@tasks.each do |task|  
task.save  
end  
?

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [April 14, 2009, 4:06pm UTC](https://www.ruby-forum.com/t/saving-mutliple-objects-to-database/165144/2 "2009-04-14T16:06:40Z")

</div>

Adam A. wrote:

> If i create in memory a hundred or so instances of a model, how do i go  
> about saving them to the database.
> 
> I have a Task Model
> 
> and say I have an array of Task obects
> 
> @tasks = […]
> 
> how do i save them
> 
> do i
> 
> @tasks.each do |task|  
> task.save  
> end  
> ?

What you can do is use the Model#create class method. You will have to  
instantiate hashes of properties instead of model objects, though. Like:

tasks = [{:name =\> ‘Go shopping’, :date =\> Time.now},{:name =\> ‘Get a  
haircut’ =\> 1.day.from\_now}] #etc  
Task.create(tasks)
