Skip to content

Instantly share code, notes, and snippets.

@huacnlee
Created August 3, 2010 01:17
Show Gist options
  • Save huacnlee/505655 to your computer and use it in GitHub Desktop.
Save huacnlee/505655 to your computer and use it in GitHub Desktop.
# 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