Saving mutliple objects to database


#1

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
?


#2

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)