Protecting an associated record from destroying -- How To and explanation?


I have a model "Pattern" with subject and body as columns
and another "Schedule" with date, time and pattern_id(for inserting an
association to Pattern table)

I had defined it as

vimal wrote:

Then i removed the pattern which was used by a schedule.
How can i avoid this action.

As the old family physician used to say, "If it hurts, don't do it."

If you want to protect against pattern deletion, you could just
eliminate the destroy method from the Pattern controller, and make the
Pattern destroy happen only through the "parent" Schedule destroy.

Another option is using the before_destroy callback to do the check: