Forum: Ruby on Rails Method not available within same module as definition

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.
163169c4734e082fac25d0397e4cadf1?d=identicon&s=25 Tyler Arrigoni (redii)
on 2009-03-27 02:34
(Received via mailing list)
# 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

--
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
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (conradwt)
on 2009-03-27 09:31
(Received via mailing list)
On Thu, Mar 26, 2009 at 6:33 PM, <tyler@oneinterface.net> wrote:

> undefined local variable or method `model_names' for
> :order => order_by_col.to_sym).collect {|s| [s.full_name, s.id]}
> models
> end
>
> 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 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]}
      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
163169c4734e082fac25d0397e4cadf1?d=identicon&s=25 Tyler Arrigoni (redii)
on 2009-03-27 10:17
(Received via mailing list)
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

-------- Original Message --------
Subject: [Rails] Re: Method not available within same module as
definition
From: Conrad Taylor <conradwt@gmail.com>
To: rubyonrails-talk@googlegroups.com
Date: 3/27/09 1:30 AM
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (conradwt)
on 2009-03-27 12:15
(Received via mailing list)
On Fri, Mar 27, 2009 at 2:16 AM, <tyler@oneinterface.net> wrote:

> 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
This topic is locked and can not be replied to.