[Rails] Child record becomes orphan
Robby Russell
robby.lists at planetargon.com
Sun Jan 1 19:02:27 GMT 2006
On Sun, 2006-01-01 at 19:52 +0100, 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?
>
> def destroy_company
> # Haven't figured out the syntax yet so don't laugh about the line below.
> if Contact.find_all(params[:company_id]) == Company.find(params[:id]) <<<
> flash[:notice] = 'This company has contacts.'
> redirect_to :action => 'show_company', :id => @company.id
> else
> Company.find(params[:id]).destroy
> redirect_to :action => 'list_companies'
> end
> end
>
> Thanx Regards,
>
> Gerard.
>
Do you intend to force the user to delete all contacts in a company
prior to deleting it? You can have AR destroy all the contacts when a
company is destroyed. See this blog entry:
http://rubyurl.com/bE4
-Robby
--
/**************************************************************
* Robby Russell, Founder & Executive Director *
* PLANET ARGON, LLC | www.planetargon.com *
* Ruby on Rails Development, Consulting, and Hosting *
* Portland, Oregon | p: 503.351.4730 | f: 815.642.4068 *
* blog: www.robbyonrails.com | book: www.programmingrails.com *
***************************************************************/
More information about the Rails
mailing list