Wednesday, 18 September 2013

Why do I get 'undefined method `>=' for nil:NilClass' in do loop?

Why do I get 'undefined method `>=' for nil:NilClass' in do loop?

I have the following data architecture:
:tplangroups has_many :tplans
:tplans belongs_to :tplangroups
:tplans has attr_accessible :favrank
I need to get the id of the tplan with the highest favrank from each
tplangroup, this routine below is how I'm trying to accomplish that:
<% @tplangroups.each_with_index do |tplangroup, index| %>
<% @highest_favrank = 0 %>
<% @highest_id = tplangroup.tplans[0] %>
<% tplangroup.tplans.each do |tplan| %>
<% if tplan.favrank >= @highest_favrank %>
<% @highest_favrank = tplan.favrank %>
<% @highest_id = tplan.id %>
<% end %>
<% end %>
#does stuff with tplangroup
<% end %>
However, I keep getting the following error:
undefined method `>=' for nil:NilClass
Any ideas? I really have no idea why it's throwing this error. I know that
all of the attributes/variables I am referencing have values, I have
tested this. I am not sure where I'm going wrong, thanks in advance!

No comments:

Post a Comment