How do I insert more than one record at a time using Mongomapper?

35 Views Asked by At

Mongmapper lets me easily create a new record as such

Track.create!({:name => "Obla di"})

Though I can't figure out how to insert more than one at a time as such

tracks = [{:name => "Obla di"}, {:name => "She Sang di"}]
Track.create!(tracks)

I know that I could just loop over the array and insert them one at a time but I would prefer to just do it in one line

1

There are 1 best solutions below

0
Jesper On BEST ANSWER

The .create! class method actually takes multiple args. It uses arbitrary arity instead of taking one array argument.

You could do this, using the splat operator:

tracks = [{:name => "Obla di"}, {:name => "She Sang di"}]
Track.create!(*tracks)

NB: As you can see from the code, this is just sugar. MongoMapper still makes one insert per document.