[Rails] Re: Re: Child record becomes orphan
Gerard
mailing at gp-net.nl
Mon Jan 2 09:44:59 GMT 2006
Jake
replies inline
> > ## With this line it works
> > @company = Company.find(params[:id])
>
> This is the first time you initialize the "@company" instance.
Am I correct when I say that after a page rendering al instances are gone (as
in removed from memory, or wherever)?
> I'm still a bit hung-over from last night, so forgive if I'm not
> answering the question correctly. :)
I know the feeling, but id didn't affect your answers so don't worry .. ;-)
> In your code above, you called Company.find(params[:id]) twice because
> you never stored the instance. You could have done:
Interesting point (performance wise). Does that mean I also do two queries to
the database?
> @company = Company.find(params[:id])
> if company.contacts.size > 0
> flash[:notice] = 'This company has contacts.'
> redirect_to :action => 'show_company', :id => @company.id
> else
> @company.destroy
> redirect_to :action => 'list_companies'
> end
>
>
> I don't recall right now, but I think you have to test
> company.contacts.size. Your test above just checks "if
> company.contacts" which I think will always evaluate true even if there
> are no contacts.
Your brilliant, I figured that one out later, because a company without
contacts also alerted that it would have contacts.
Learned a lot. 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