开发者

rails3 working in sandbox environment

开发者 https://www.devze.com 2023-03-05 04:49 出处:网络
Is there any way of switching on/off sanbox environment for activerecord in rails? I\'m developing a rails application. I have a case where I need to load data from xls and csv files. Each row of the

Is there any way of switching on/off sanbox environment for activerecord in rails?

I'm developing a rails application. I have a case where I need to load data from xls and csv files. Each row of the table is inserted to database separately. When some mistake is found in any row, I need to cancel all previously inserted rows and show an error message. The perfect solution would be to "switch on" sandbox environment for activerecord, insert data, then if every line is correct make the the previous insertions "happen", and then "switch off" the sandbox environment. Is开发者_Go百科 this possible in rails?


It depends on what time do those insert take. If you don't mind locking the database for that time, you can use transactions:

MyModel.transaction do
   objects.each do |object|
      object.attributes = values
      object.save!
   end
end

The transaction rescues all exceptions by itself and if any occurs, all previous saves are rolled back.

0

精彩评论

暂无评论...
验证码 换一张
取 消