Re: activerecord transaction

AndyV
2008-06-16T16:15:36Z

Depending on the nature of your relationships you might be able to do
this (which has automatic transaction wrapping on the DB):

@user = User.new(params[:user])
@user.build_user_detail(params[:user_detail])
@user.build_user_tree
@user.save

If either UserDetail or UserTree uses a has_many relationship then you
can use @user.user_xxxs.build(params[:user_xxx])

On Jun 16, 11:35 am, Ajit
<ajitscor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
> hello all
>
> I have one form.. which provide data to three tables, 3 Models. i am
> trying to setup a transaction for save statement. seems like i am
> missing something.. as this is not working properly. following is my
> code:
>
> User.transaction do
>                                 @user = User.new(params[:user])
>
>                                 @user.save
>
>                                 @user_details =
UserDetail.new(params[:user_detail])
>                                 @user_details.user_id = @user.id
>                                 @user_details.save
>
>                                 @user_tree = UserTree.new()
>                                 @user_tree.user_id = @user.id
>                                 @user_tree.save
> end
>
> if something goes wrong then User table is not creating new row but
> other two tables get new rows.
>
> any suggestions.
>
> Thanks in advance
>
> Ajit