DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

SQL Transaction In Rails

05.11.2007
| 8565 views |
  • submit to reddit
        
def fetch_value
	sql = ActiveRecord::Base.connection();
	sql.execute "SET autocommit=0";
	sql.begin_db_transaction
	id, value =
	sql.execute("SELECT id, value FROM sometable WHERE used=0 LIMIT 1 FOR UPDATE").fetch_row;
	sql.update "UPDATE sometable SET used=1 WHERE id=#{id}";
	sql.commit_db_transaction
 
	value;
end