Hi all,
Im trying to add new rows to multiple tables and need a little help
understanding transactions.
My goal is to create a new user account, account holder and blog in one
go. If either of the saes fail I would like the error messages returned
to use in my views.
I have the following method… is this the best way to achieve the goal.
It seems a little over complicated
******* CONTROLLER *******
def signup
case request.method
when :post
@account = Account.new(params[:account])
@account_holder = AccountHolder.new(params[:account_holder])
@blog = Blog.new(params[:blog])
@account_holder.display_name =
params[:account_holder][:first_name] + " " +
params[:account_holder][:last_name]
begin
Account.transaction do
# blog > membership < group
@blog.users << @account_holder
@blog.save!
# account_holder > membership < group
@account_holder.account = @account
@account_holder.save!
@account.save!
end
rescue Exception => e
#logger.error("message for the log file #{e.message}")
#flash[:notice] = e.message
render :action => 'signup' and return
end
redirect_to :action => 'list'
end
end