Forum: Ruby on Rails acts_as_tree problem accessing parent object

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
10a446a0c7784a33355c98fbed54c4d3?d=identicon&s=25 Nate C. (natcon67)
on 2006-06-13 06:08
i have a model called certification_types that i've declared as
acts_as_tree.  I have a model called certification that belongs_to
:certification_type .

class CertificationController < ApplicationController
    .....
    def summary_list
	@certifications = Certification.find(:all, :order=>'date')
    end
end

summary_list.rhtml
 <% for certification in @certifications %>
		<%= certification.certification_type.parent.id %>
 <% end %>


so, the line above (<%=
certification.certification_type.parent.type_description %> ) does not
work; it says The error occured while evaluating nil.type_description.

if i do a debug(certification.certification_type.parent) it shows that i
have a hash of values:
--- !ruby/object:CertificationType
attributes:
  type_description: Allergy & Immunology
  id: "12"
  parent_id: "11"

can anyone help me with how to access the parent type_description when
looping through my collection?

Thanks,

Nate
shai (Guest)
on 2006-06-13 11:23

>  <% for certification in @certifications %>
> 		<%= certification.certification_type.parent.id %>
>  <% end %>


...my guess is that the reason you are getting nil is because there must
be cases in the loop that certification does not have a certification
type - - did you put the
"debug(certification.certification_type.parent)" AFTER the loop? , if
so, you may have gotten the last value of the loop - which does have a
certification type, but other objects in the loop do not, therefore
returning a nil object - -
the best way to see if this is correct or not is to put the debug(..)
inside the loop and see what it returns...

shai
10a446a0c7784a33355c98fbed54c4d3?d=identicon&s=25 Nate C. (natcon67)
on 2006-06-13 16:41
shai wrote:
>
>
>>  <% for certification in @certifications %>
>> 		<%= certification.certification_type.parent.id %>
>>  <% end %>
>
>
> ...my guess is that the reason you are getting nil is because there must
> be cases in the loop that certification does not have a certification
> type - - did you put the
> "debug(certification.certification_type.parent)" AFTER the loop? , if
> so, you may have gotten the last value of the loop - which does have a
> certification type, but other objects in the loop do not, therefore
> returning a nil object - -
> the best way to see if this is correct or not is to put the debug(..)
> inside the loop and see what it returns...
>
> shai

Shai, I cant believe i missed that!  Thank you very much for responding.

Nate
shai (Guest)
on 2006-06-13 17:13
Anytime ;-)


Shai
This topic is locked and can not be replied to.