[Rails] Re: Child record becomes orphan
Jake Janovetz
jakejanovetz at yahoo.com
Sun Jan 1 19:00:27 GMT 2006
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
--
Posted via http://www.ruby-forum.com/.
More information about the Rails
mailing list