Forum: Ruby on Rails When saving parent fails, children will still be updated

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
42a57399af9faa20f32dc01825857b53?d=identicon&s=25 Patrick B. (bazzel)
on 2006-06-13 10:17
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
Mark Reginald James (Guest)
on 2006-06-14 09:19
(Received via mailing list)
Patrick Baselier 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.
Isak Hansen (Guest)
on 2006-06-14 11:31
(Received via mailing list)
On 6/13/06, Patrick Baselier <patrick.baselier@gmail.com> 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
42a57399af9faa20f32dc01825857b53?d=identicon&s=25 Patrick B. (bazzel)
on 2006-06-14 11:42
Isak Hansen wrote:
> Which db are you using?

I'm using mySQL 4.1 and created InnoDB tables. I still have to try Mark
Reginald James' suggestion, I think that'll do it. Let you know later
today.
42a57399af9faa20f32dc01825857b53?d=identicon&s=25 Patrick B. (bazzel)
on 2006-06-14 12:26
Mark Reginald James wrote:
> You have to raise an exception to rollback a transaction.

Thanks Mark, I replace my code and it works.

Great!
This topic is locked and can not be replied to.