save an active records array

前端 未结 6 928
梦如初夏
梦如初夏 2021-02-05 07:28

I have an array like this

a = []

a << B.new(:name => \"c\")
a << B.new(:name => \"s\")
a << B.new(:name => \"e\")
a << B.new(:n         


        
6条回答
  •  耶瑟儿~
    2021-02-05 08:04

    B.transaction do
      a.each(&:save!)
    end
    

    This will create a transaction that loops through each element of the array and calls element.save on it.

    You can read about ActiveRecord Transactions and the each method in the Rails and Ruby APIs.

提交回复
热议问题