Hi, I have a problem where I need an auto-incremented id back from the
database for a statement that has yet to be committed. The operation
that uses the id may fail, and if so, I need to rollback the database.
I can’t figure out how to send only the statement to the database
without the commit without sending over hard coded SQL. This is what I
started out with:
1 def add_album
2 Album.transaction do
3 @page = Page.find(params[:id])
4 @album = Album.new(params[:album]) #creates a new Album in the
6 linking = Linking.new(:page_id => params[:id])
7 linking.linkable = @album
9 create_path(@album.id) #makes a directory using the album id
11 linking.save! #sends statement AND commits?
14 redirect_to :action => ‘index’
The problem with this is that @album’s id has not yet been populated.
Moving the create_path call past the object saves solves that, but now
the rows are committed to the database and I can’t roll them back.
I’m sure there’s a way to solve this, but I can’t seem to figure it out
from here. Where exactly will Rails send the statements over? Is the
“save!” both sending statements AND committing?