# Method not available within same module as definition

**URL:** <https://www.ruby-forum.com/t/method-not-available-within-same-module-as-definition/164029>\
**Category:** Rails\
**Created:** [March 27, 2009, 2:34am UTC](https://www.ruby-forum.com/t/method-not-available-within-same-module-as-definition/164029 "2009-03-27T02:34:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![redii](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@redii](https://www.ruby-forum.com/u/redii)\
**Post date:** [March 27, 2009, 2:34am UTC](https://www.ruby-forum.com/t/method-not-available-within-same-module-as-definition/164029/1 "2009-03-27T02:34:29Z")

</div>

# 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](http://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: removed\_email\_address@domain.invalid  
GnuPG: [http://www.oneinterface.net/crypt/tyler.asc](http://www.oneinterface.net/crypt/tyler.asc)

---

<div class="post-metadata">

**Author:** ![redii](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@redii](https://www.ruby-forum.com/u/redii)\
**Post date:** [March 27, 2009, 9:31am UTC](https://www.ruby-forum.com/t/method-not-available-within-same-module-as-definition/164029/2 "2009-03-27T09:31:11Z")

</div>

On Thu, Mar 26, 2009 at 6:33 PM, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://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: removed\_email\_address@domain.invalid  
> GnuPG: [http://www.oneinterface.net/crypt/tyler.asc](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

---

<div class="post-metadata">

**Author:** ![redii](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@redii](https://www.ruby-forum.com/u/redii)\
**Post date:** [March 27, 2009, 10:17am UTC](https://www.ruby-forum.com/t/method-not-available-within-same-module-as-definition/164029/3 "2009-03-27T10:17:26Z")

</div>

Conrad,

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

Thanks,

Tyler Arrigoni  
President

[OneInterface.net](http://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: removed\_email\_address@domain.invalid  
GnuPG: [http://www.oneinterface.net/crypt/tyler.asc](http://www.oneinterface.net/crypt/tyler.asc)

-------- Original Message --------  
Subject: [Rails] Re: Method not available within same module as  
definition  
From: Conrad T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: removed\_email\_address@domain.invalid  
Date: 3/27/09 1:30 AM

---

<div class="post-metadata">

**Author:** ![redii](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@redii](https://www.ruby-forum.com/u/redii)\
**Post date:** [March 27, 2009, 12:15pm UTC](https://www.ruby-forum.com/t/method-not-available-within-same-module-as-definition/164029/4 "2009-03-27T12:15:47Z")

</div>

On Fri, Mar 27, 2009 at 2:16 AM, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [OneInterface.net](http://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: removed\_email\_address@domain.invalid  
> GnuPG: [http://www.oneinterface.net/crypt/tyler.asc](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
