[Rails] Re: Child record becomes orphan
Gerard
mailing at gp-net.nl
Sun Jan 1 21:36:25 GMT 2006
Jake,
Not to spam your mailbox but ... Solved it and still don't know why. The line
below '##' did the trick. But the company's id is known (since that the one
I'm attempting to delete) so why should I init another object?
Thanx again (and again) .. :-)
def destroy_company
if Company.find(params[:id]).contacts
flash[:notice] = 'This company has contacts.'
## With this line it works
@company = Company.find(params[:id])
redirect_to :action => 'show_company', :id => @company.id
else
Company.find(params[:id]).destroy
redirect_to :action => 'list_companies'
end
end
Thanx
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