[Rails] Child record becomes orphan

Gerard mailing at gp-net.nl
Sun Jan 1 18:52:33 GMT 2006


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.

-- 
"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