Problem iterating over and array of objects

what am i missing here . the line a.each { |B| fails with the error

NoMethodError: You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.+
from (irb):4:in `create_menu’
from (irb):17

but a is an array of menu objects… HELP!!!

def create_menu(menu)
a = Menu.find(:all, :conditions => “menu = '” + menu + “’”, :order
=> “‘order’” )
if a.size then
ret += “

    \n”
    a.each { |b|
    ret += “
  • \n”
    ret += link_to h(b.name), :action => b.controller
    +"/"+b.action, :id => b.name
    ret += “\n”
    ret += create_menu(b.submenu)
    ret += “
  • \n”
    }
    ret += “
\n”
end
end

On 18 Dec 2007, at 19:39, spokra wrote:

what am i missing here . the line a.each { |B| fails with the error

NoMethodError: You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.+
from (irb):4:in `create_menu’
from (irb):17

a may be, but ret isn’t set to anything, and a quirk of ruby means
that it will be nil.

In addition, using find like that leaves you open to sql injection
problems and your if condition is bogus. Unlike some languages 0 is
not synonymous with false

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs