Thomas Davis
Thomas Davis

Reputation: 1896

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

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

Upvotes: 1

Views: 34

Answers (1)

Jesper
Jesper

Reputation: 4555

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.

Upvotes: 1

Related Questions