1

gemを使用する場合Moped、ハッシュの配列を次のように格納できます。

users = [{username: "ben", password: "123456", type: "admin" }, {username: "joe", password: "abcd1234" }]
Mongoid::Sessions.default["collection"].insert(users)

Mongoid ドキュメントでは、次のようになります。

class User
  field :username, type: String
  field :password, type: String
end

users.each { |user_hash| User.create(user_hash) }

これは、それぞれの挿入操作を意味します。シングルオペレーション方式を維持する方法をご存知ですか?での取引のようなものでしょActiveRecordうか?

4

1 に答える 1

1

ドキュメントをハッシュに変換し、単一の呼び出しで挿入できます#create:

User.create(users.map(&:attributes))
于 2015-08-05T16:53:04.370 に答える