2010-06-30 130 views
3

我的例子DataMapper的類DataMapper,如何創建多個記錄?

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

而且我有一個數組ID的UID我想補充。

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

如果我想要的陣列我會做類似

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

在搜索任何標識的是有沒有辦法做同樣的創建多個記錄,例如:

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

解決的辦法是:

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

但這樣做效率不高,必須有更好的方法。

回答

0

我不知道SQL層上的任何批量插入操作。考慮data_mapper是抽象層的SQL數據庫。請注意,每個數據庫作者/製造商都實施了自己的工具(例如,Oracle的SQLLDR,MS SQL Server的BCP)。

抽象層沒有辦法將它帶入單個接口。