Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

SQL Transaction In Rails

DZone's Guide to

SQL Transaction In Rails

·
Free Resource

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
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}