DataMapper, how to create multiple records?

My datamapper class class

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

And I have the id id of the array that 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 find any of the id in the array, I would do something like

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

Is there a way to do the same to create multiple entries, such as:

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

The way around:

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

But this cannot be effective; there must be a better way.

+3
source share
2 answers

There is no easier way in DataMapper. Most ORMs do not worry about bulk operations because they often require database-specific implementations. One of the few ORMs that does this is SQLAlchemy: http://www.sqlalchemy.org/docs/05/sqlexpression.html#executing-multiple-statements

+2

- SQL. SQL, data_mapper . , / (, SQLLDR Oracle, BCP MS SQL Server).

.

0

Source: https://habr.com/ru/post/1752384/


All Articles