Forum: Ruby on Rails How to restrict deletion of ActiveRecord objects if there de

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.
gneeot (Guest)
on 2007-07-29 19:13
(Received via mailing list)
Hi guys,

Trying to find some way to restrict deletion of ActiveRecord objects
if there dependent records.

I'm thinking of using before_destroy for it, but don't know how to
"cancel" deletion in my code in before_destroy "decides" that deletion
should be restricted.


Thank you in advance.

--
Regards
Yuriy Padlyak
Adam G. (Guest)
on 2007-07-30 03:18
gneeot wrote:
>
> I'm thinking of using before_destroy for it, but don't know how to
> "cancel" deletion in my code in before_destroy "decides" that deletion
> should be restricted.
>

According to the Rails doc, all you need to do is return false from your
before_destroy method. Here's the relevant section:

  # == Cancelling callbacks
  #
  # If a before_* callback returns false, all the later callbacks and
the associated action are cancelled. If an after_* callback returns
  # false, all the later callbacks are cancelled. Callbacks are
generally run in the order they are defined, with the exception of
callbacks
  # defined as methods on the model, which are called last.


Hope this helps,

Adam
Bill W. (Guest)
on 2007-07-30 06:43
(Received via mailing list)
Hi Yuryi,

> Trying to find some way to restrict deletion of ActiveRecord objects
> if there dependent records.
>
> I'm thinking of using before_destroy for it, but don't know how to
> "cancel" deletion in my code in before_destroy "decides" that deletion
> should be restricted.

I'm pretty sure that if a "before_" filter returns false, the whole
action
is canceled.  Check the class docs at api.rubyonrails.org to be sure.

HTH,
Bill
gneeot (Guest)
on 2007-07-30 12:43
(Received via mailing list)
thanks, will try it.
gneeot (Guest)
on 2007-08-06 20:23
(Received via mailing list)
it works great, thank you.
This topic is locked and can not be replied to.