Re: activerecord transaction

Frederick Cheung
2008-06-16T16:49:10Z

On 16 Jun 2008, at 16:35, Ajit wrote:
>
> 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.

What are you expecting to happen? are you expecting this to rollback
if @user.save fails? Rollback only happens if an exception is thrown,
and save just returns false if validation fails. save! on the other
hand throws an exception when validation fails

Fred
>
>
> any suggestions.
>
> Thanks in advance
>
> Ajit
> >