[Rails] Re: Child record becomes orphan

Gerard mailing at gp-net.nl
Sun Jan 1 20:25:20 GMT 2006


Jake,

I've got what I'm looking for.

It will be the if statement, then a user confirmation, the a recursive 
removall.

Thanx a lot!

Regards,

Gerard.



On Sunday 01 January 2006 20:00, Jake Janovetz tried to type something like:
> Gerard wrote:
> > Hi all,
> >
> > I've got these company (has) contacts relational tables. I want to make
> > sure a
> > company can't be deleted when it still has contacts. (because I can now,
> > which results in nil like errors when listing the contacts ...
> > obviously)
> >
> > Are there any helpers to do so, is Rails aware of this? Or do I need to
> > put in
> > a condition before destroying the company record in question myself?
>
> If you setup your associations within the model correctly, you will be
> able to do:
>
> if Company.find(params[:id]).contacts.size > 0
>    ...Don't delete quite yet...
> end
>
> However, you can also set the :dependent => true flag in the association
> which will then delete associated contacts before deleting the company.
>
>    Jake

-- 
"Who cares if it doesn't do anything?  It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process ..."

My $Grtz =~ Gerard;
~
:wq!





More information about the Rails mailing list