# When saving parent fails, children will still be updated

**URL:** <https://www.ruby-forum.com/t/when-saving-parent-fails-children-will-still-be-updated/63223>\
**Category:** Rails\
**Created:** [June 13, 2006, 10:17am UTC](https://www.ruby-forum.com/t/when-saving-parent-fails-children-will-still-be-updated/63223 "2006-06-13T10:17:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bazzel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@bazzel](https://www.ruby-forum.com/u/bazzel)\
**Post date:** [June 13, 2006, 10:17am UTC](https://www.ruby-forum.com/t/when-saving-parent-fails-children-will-still-be-updated/63223/1 "2006-06-13T10:17:58Z")

</div>

Hi all,

When submitting a master/detail form I first want to handle the children  
before saving the parent.  
There are has\_many and belongs\_to relations between the parent and the  
children, so I can edit the children using (psuedo-)code like:  
child.destroy for deleting, child.update\_attributes for updating and  
@parent.children.push(child) for adding child-records.  
I update the parent by using:  
if @parent.update\_attributes(params[:parent])  
flash[:notice] = ‘Parent was successfully updated.’  
redirect\_to(:action =\> “list”)  
else  
render :action =\> ‘edit’  
end # if

The problem I’m facing at the moment is that when updating the parent  
fails, the chances made to the children are still saved. I tried to  
include the code in a transaction:  
Parent.transaction do

# all code

end

but this makes no difference.

What am I doing wrong? Hope someone can help me out on this.

Thanks in advance,

Patrick

---

<div class="post-metadata">

**Author:** ![bazzel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@bazzel](https://www.ruby-forum.com/u/bazzel)\
**Post date:** [June 14, 2006, 9:19am UTC](https://www.ruby-forum.com/t/when-saving-parent-fails-children-will-still-be-updated/63223/2 "2006-06-14T09:19:12Z")

</div>

Patrick B. wrote:

> else  
> but this makes no difference.  
> You have to raise an exception to rollback a transaction.  
> So you must code something like:

begin  
Parent.transaction do  
# update children  
@parent.attributes = params[:parent]  
@parent.save!  
flash[:notice] = ‘Parent was successfully updated.’  
redirect\_to :action =\> “list”  
end  
rescue  
render :action =\> ‘edit’  
end

To avoid a transaction you’d have to check everything for  
validity before you start any saving, and then hope there’s  
no database errors.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![bazzel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@bazzel](https://www.ruby-forum.com/u/bazzel)\
**Post date:** [June 14, 2006, 11:31am UTC](https://www.ruby-forum.com/t/when-saving-parent-fails-children-will-still-be-updated/63223/3 "2006-06-14T11:31:56Z")

</div>

On 6/13/06, Patrick B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,
> 
> When submitting a master/detail form I first want to handle the children  
> before saving the parent.  
> _snip_  
> The problem I’m facing at the moment is that when updating the parent  
> fails, the chances made to the children are still saved. I tried to  
> include the code in a transaction:  
> Parent.transaction do
> 
> # all code
> 
> end
> 
> but this makes no difference.
> 
> What am I doing wrong? Hope someone can help me out on this.

Which db are you using? A certain popular ‘database’ doesn’t enable  
transactions by default.

Isak

---

<div class="post-metadata">

**Author:** ![bazzel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@bazzel](https://www.ruby-forum.com/u/bazzel)\
**Post date:** [June 14, 2006, 11:42am UTC](https://www.ruby-forum.com/t/when-saving-parent-fails-children-will-still-be-updated/63223/4 "2006-06-14T11:42:09Z")

</div>

Isak H. wrote:

> Which db are you using?

I’m using mySQL 4.1 and created InnoDB tables. I still have to try Mark  
Reginald J.’ suggestion, I think that’ll do it. Let you know later  
today.

---

<div class="post-metadata">

**Author:** ![bazzel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@bazzel](https://www.ruby-forum.com/u/bazzel)\
**Post date:** [June 14, 2006, 12:26pm UTC](https://www.ruby-forum.com/t/when-saving-parent-fails-children-will-still-be-updated/63223/5 "2006-06-14T12:26:11Z")

</div>

Mark Reginald J. wrote:

> You have to raise an exception to rollback a transaction.

Thanks Mark, I replace my code and it works.

Great!
