Created
August 3, 2010 01:17
-
-
Save huacnlee/505655 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rails 事务 | |
begin | |
Post.transaction do | |
# 删除旧数据 | |
Post.delete_all | |
# 例子 新数据 | |
datas [{:title => "title1", :slug => "title-1"},{:title => "Title 2", :slug => "Title 2"}] | |
counter = 0 | |
datas.each do |d| | |
post = Post.new(d) | |
if not post.save | |
raise "*** #{post.errors.full_messages}" | |
end | |
end | |
end | |
puts "=== Done" | |
rescue Exception => ex | |
puts "*** transaction abored!" | |
puts "*** errors: #{ex.message}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment