Okay, I'll trust you guys know better than I do.
How do I do this with a block?
def signup
# It gets a little tricky, since we want to create either both an
account and user or neither
@account = Account.new(params[:account])
@user = User.new(params[:user])
@user.login = @account.code # For now, we keep a 1-to-1 between
these
@user.real_name = @account.name
begin
# TODO begin transaction
if (@user.valid?)
@account.user = @user # Don't assign until we know it is valid -
ActiveRecord sometimes saves on assign
if (@account.valid?) # We need to do this in two steps, since
@account won't be valid without a user
@user.save!
@account.save!
# TODO end transaction
flash[:notice] = 'Account added.'
redirect_to :action => 'list'
return
end
end
# TODO rollback transaction
render :action => 'new'
# TODO rescue exceptions
end
end
(Okay, I admit it, yes, this is part of a rails app...)