Array of hashes `create` many ActiveRecord objects
Generally, you use the create
method of ActiveRecord objects to create an object by passing a hash of attributes as the argument.
Thing.create(color: 'green', status: 'active')
You can also pass an array of hashes to create
:
things = [
{
color: 'blue',
status: 'pending'
},
{
color: 'green',
status: 'active'
]
created_things = Thing.create(things)
One disappointing thing is that this does not batch the insert statements. It is still just one insert statement per object, but it might make your code simpler in some cases.
Tweet