eiji
March 12th, 2006, 06:44 AM
hi there. 2 days experience in ruby on rails and im loving it already. though i have a lot of questions, i mean a lot. as others i dont really seem to get. and my head is aching as they want me to create apps very fast, and i havent read much..
one question is the transactions.. could someone help me here?
i must make a transaction here that when one saving into database fails, it would rollback all the transactions made on that block of transaction.
my codes look like this :
/controller/signup
class SignupController < ApplicationController
def save
@user = User.new()
@user.usertype_id = 1
if @user.save
@memberaccount = Memberaccount.new(params[:memberaccount])
@memberaccount.user_id = @user.id
@memberaccount.sysdate = Date.today
if @memberaccount.save
redirect_to :action => 'thankyou'
else
# flash[:notice] = 'an error has occured when signing up. please retry1'
render :action => 'index'
end
else
flash[:notice] = 'an error has occured when signing up. please retry2'
render :action => 'index'
end
end
end
now here is a two part transaction that first saves into the users database and when it succeeds and then again enters more in the memberaccount database. now my question is when the "@memberaccount.save" had an error.. how do i rollback the save that occured on the users?
i have seen some transactions stuff but how do i squeeze this in my codes??
e.g.
Account.transaction(account1, account2) do
account1.withdraw(100)
account2.deposit(100)
end
e.g.2
Account.transaction do
User.transaction do
yada_yada
end
end
thanks for the help :D
one question is the transactions.. could someone help me here?
i must make a transaction here that when one saving into database fails, it would rollback all the transactions made on that block of transaction.
my codes look like this :
/controller/signup
class SignupController < ApplicationController
def save
@user = User.new()
@user.usertype_id = 1
if @user.save
@memberaccount = Memberaccount.new(params[:memberaccount])
@memberaccount.user_id = @user.id
@memberaccount.sysdate = Date.today
if @memberaccount.save
redirect_to :action => 'thankyou'
else
# flash[:notice] = 'an error has occured when signing up. please retry1'
render :action => 'index'
end
else
flash[:notice] = 'an error has occured when signing up. please retry2'
render :action => 'index'
end
end
end
now here is a two part transaction that first saves into the users database and when it succeeds and then again enters more in the memberaccount database. now my question is when the "@memberaccount.save" had an error.. how do i rollback the save that occured on the users?
i have seen some transactions stuff but how do i squeeze this in my codes??
e.g.
Account.transaction(account1, account2) do
account1.withdraw(100)
account2.deposit(100)
end
e.g.2
Account.transaction do
User.transaction do
yada_yada
end
end
thanks for the help :D