Dublinclontarf
Dublinclontarf

Reputation: 99

DataMapper, how to create multiple records?

My example datamapper class

class Simple
  include DataMapper::Resource
  property  :id, Serial
  property  :uid, Integer
end

And I have an array id uid's I would like to add.

items=[1,2,3,4,5,6,7,8,9,1,1,1,2,2,2,3]

If I wanted to search for any of the id's in the array I would do something like

Simple.all(:uid.in=>items) 

Is there a way to do the same for creating multiple records such as:

Simple.create(:uid=>items) #this doesn't work by the way

A way around this is:

items.each{|item|Simplel.create(:uid=>item)} 

But this can't be efficient,there has to be a better way.

Upvotes: 3

Views: 1020

Answers (2)

Steffen Roller
Steffen Roller

Reputation: 3494

I'm not aware of any bulk insert operations on the SQL layer. Think of the SQL database for which data_mapper is the abstraction layer. Mind you that each database author/manufacturer implemented its own tool (eg. SQLLDR for Oracle, BCP for MS SQL Server).

There is no way for the abstraction layer to bring this into a single interface.

Upvotes: 0

jhickner
jhickner

Reputation: 1053

There's no easier way in DataMapper. Most ORMs don't bother with bulk operations because they often require database-specific implementations. One of the few ORMs that does is SQLAlchemy: http://www.sqlalchemy.org/docs/05/sqlexpression.html#executing-multiple-statements

Upvotes: 2

Related Questions