save an active records array

前端 未结 6 802
梦如初夏
梦如初夏 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:14

    I know this is an old question but I'm suprised no one thought of this:

    B.transaction do
      broken = a.reject { |o| o.save }
      raise ActiveRecord::Rollback if broken.present?
    end
    
    if broken.present?
      # error message
    end
    

提交回复
热议问题