Method not available within same module as definition

# This will probably be embarrassingly obvious. It acts as if it's an
# evaluation order issue (like the method call is happening prior to the
# method definition). But that isn't the case. The model_names method
# works fine on the console. I'm missing a core concept obviously.

# Error message from test
tylers-mbpro:test tyler$ ruby helpers/application_helper_test.rb
/Users/tyler/versioned/tma-code/rails/facilit-e/app/helpers/application_helper.rb:23:
undefined local variable or method `model_names' for
ApplicationHelper:Module (NameError)

# ApplicationHelper
module ApplicationHelper

def self.make_user_select_collects(assoc_id_param)
module_eval %{
def select_collect_by_#{assoc_id_param}(klass, assoc_id,
order_by_col='full_name')
klass.find_all_by_#{assoc_id_param}(assoc_id,
:order => order_by_col.to_sym).collect {|s| [s.full_name, s.id]}
end
}
end

def self.model_names
models = Array.new
Dir["app/models/**/*.rb"].each do |f|
models <<
File.basename(f).sub(/.rb$/,'').titlecase.gsub(/\s/,'').constantize
end
models
end

ids = Array.new
model_names.each do |klass| #model_names
klass.column_names.grep(/_id$/).each do |assoc_id|
unless ids.member?(assoc_id)
ids << assoc_id
make_user_select_collects(assoc_id)
end
end
end

end

# Thanks for any help

Tyler, I wasn’t able to reproduce the error message above using this simplified example:

module ApplicationHelper

def self.make_user_select_collects(assoc_id_param)

module_eval %{

def select_collect_by_#{assoc_id_param}(klass, assoc_id,

order_by_col=‘full_name’)

klass.find_all_by_#{assoc_id_param}(assoc_id,

:order => order_by_col.to_sym).collect {|s| [s.full_name, [s.id](http://s.id)]}

end

}

end

def self.model_names

models = Array.new

Dir[“app/models/**/*.rb”].each do |f|

models <<

File.basename(f).sub(/.rb$/,’’).titlecase.gsub(/\s/,’’).constantize

end

models

end

ids = Array.new

model_names.each do |klass| #model_names

klass.column_names.grep(/_id$/).each do |assoc_id|

unless ids.member?(assoc_id)

ids << assoc_id

make_user_select_collects(assoc_id)

end

end

end

puts “Model #{self} completed”

end

darnoc-desktop:app conradwt$ ruby helpers/application_helper.rb

=> Model ApplicationHelper completed

BTW, I used Ruby 1.9.1 to execute the above code. I’ll try the above on my laptop using

Ruby 1.8.6 to see if it’s consistent with Ruby 1.9.1.

-Conrad

Conrad,

Interesting...please do let me know your results with 1.8.6.

Thanks,

Tyler Arrigoni
President

OneInterface.net Network Engineering, Inc.
7475 North Palm, Suite 105, Fresno, CA 93711
Voice: 559-437-3750 ext. 110
Cell: 559-696-0400
Fax1: 866-691-5145 (confidential)
Fax2: 559-437-3754 (non-confidential)
Email: tyler@oneinterface.net
GnuPG: http://www.oneinterface.net/crypt/tyler.asc

Tyler, I’m seeing the same result using Ruby 1.8.6. BTW, here’s the exact version that I’m running in regards to 1.8.6:

ruby 1.8.6 (2008-08-11 patchlevel 287) [i686-darwin9.6.0]

-Conrad