# Module and Model

**URL:** <https://www.ruby-forum.com/t/module-and-model/94049>\
**Category:** Rails\
**Created:** [April 19, 2006, 5:19am UTC](https://www.ruby-forum.com/t/module-and-model/94049 "2006-04-19T05:19:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomask](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/85f322/32.png) [@thomask](https://www.ruby-forum.com/u/thomask)\
**Post date:** [April 19, 2006, 5:19am UTC](https://www.ruby-forum.com/t/module-and-model/94049/1 "2006-04-19T05:19:39Z")

</div>

I am getting the following error

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/base.rb:1129:in  
`method_missing': undefined method`directory?’ for Msg::File:Class  
(NoMethodError)

I have a model called Msg::File in models/msg/file.rb, and I have a file  
in lib/msg/helper.rb which references to both Msg::File and  
File.directory?

How do I help activerecord to use the system File instead of my  
activerecord object Msg::File?

---

<div class="post-metadata">

**Author:** ![thomask](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/85f322/32.png) [@thomask](https://www.ruby-forum.com/u/thomask)\
**Post date:** [April 19, 2006, 5:39am UTC](https://www.ruby-forum.com/t/module-and-model/94049/2 "2006-04-19T05:39:22Z")

</div>

I might be way off here (i’ve only had to do that kind of thing once)  
but try ::File.directory?

Cheers,  
Dan

---

<div class="post-metadata">

**Author:** ![thomask](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/85f322/32.png) [@thomask](https://www.ruby-forum.com/u/thomask)\
**Post date:** [April 19, 2006, 6:12am UTC](https://www.ruby-forum.com/t/module-and-model/94049/3 "2006-04-19T06:12:04Z")

</div>

Hi Dan,

Thanks for the suggestion. Unfortunately, same error.

---

<div class="post-metadata">

**Author:** ![thomask](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/85f322/32.png) [@thomask](https://www.ruby-forum.com/u/thomask)\
**Post date:** [April 19, 2006, 7:11am UTC](https://www.ruby-forum.com/t/module-and-model/94049/4 "2006-04-19T07:11:11Z")

</div>

Hi Thomas,

you’ve got me wondering whether you’ve correctly defined your  
Msg::File class…

in msg/file.rb does it look like this?

class Msg::File \< ActiveRecord::Base  
end

or this?

module Msg  
class File \< ActiveRecord::Base  
end  
end

Either of those ought to work.

## Regards, Trevor

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)

---

<div class="post-metadata">

**Author:** ![thomask](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/85f322/32.png) [@thomask](https://www.ruby-forum.com/u/thomask)\
**Post date:** [April 19, 2006, 4:04pm UTC](https://www.ruby-forum.com/t/module-and-model/94049/5 "2006-04-19T16:04:03Z")

</div>

Hey,

On 18-Apr-06, at 11:05 PM, Thomas Kwan wrote:

> Trevor,
> 
> I have my class defined as
> 
> class Msg::File \< ActiveRecord::Base  
> set\_table\_name “msg\_files”  
> end

I tried this out here (rails 1.1) and in the console it works fine  
for me in the console:

|\>\> Msg::File.new  
|=\> #\<Msg::File:0x2213650 @new\_record=true, @attributes={“name”=\>""}\>  
|\>\> File.directory? ‘/tmp/’  
|=\> true

> I remember I’ve read from somewhere that the model name (i.e. File)  
> cannot be the same as the module name of your controller. I saw  
> that happened before. But in this case, this model name is the  
> same as one of the module name of a system library.

The controller-vs-model name clashes shouldn’t be the issue here.

I know it’s already been suggested but my instinct is to think you’ve  
got a bit  
of code that’s interpreting a bare ‘File’ as ‘Msg::File’ when you  
really mean ‘::File’.

Check each line of the backtrace to make sure you haven’t missed  
something.

Trev

---

<div class="post-metadata">

**Author:** ![thomask](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/85f322/32.png) [@thomask](https://www.ruby-forum.com/u/thomask)\
**Post date:** [April 19, 2006, 8:05am UTC](https://www.ruby-forum.com/t/module-and-model/94049/6 "2006-04-19T08:05:41Z")

</div>

Trevor,

I have my class defined as

class Msg::File \< ActiveRecord::Base  
set\_table\_name “msg\_files”  
end

I remember I’ve read from somewhere that the model name (i.e. File)  
cannot be the same as the module name of your controller. I saw  
that happened before. But in this case, this model name is the  
same as one of the module name of a system library.

---

<div class="post-metadata">

**Author:** ![thomask](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/85f322/32.png) [@thomask](https://www.ruby-forum.com/u/thomask)\
**Post date:** [April 19, 2006, 4:37pm UTC](https://www.ruby-forum.com/t/module-and-model/94049/7 "2006-04-19T16:37:32Z")

</div>

Trevor,

I am on rails 1.1 also. I can reproduce the problem by doing the  
following:

- Create app/models/msg/file.rb

- Create app/lib/msg/my\_helper.rb

- Create app/script/driver.rb

Thanks

---

<div class="post-metadata">

**Author:** ![thomask](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/85f322/32.png) [@thomask](https://www.ruby-forum.com/u/thomask)\
**Post date:** [April 19, 2006, 6:19pm UTC](https://www.ruby-forum.com/t/module-and-model/94049/8 "2006-04-19T18:19:15Z")

</div>

Trevor,

Yes, that works.That was also suggested by Dan. It wasnt working when I  
tried this earlier on. Thanks to both of you!

---

<div class="post-metadata">

**Author:** ![thomask](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/85f322/32.png) [@thomask](https://www.ruby-forum.com/u/thomask)\
**Post date:** [April 19, 2006, 6:11pm UTC](https://www.ruby-forum.com/t/module-and-model/94049/9 "2006-04-19T18:11:27Z")

</div>

Hi Thomas,

change your code as follows:

def reproduce\_error  
Msg::File.new  
::File.directory? ‘/tmp’  
end

Any code that lives in the Msg module _must_ use the fully qualified  
name  
for ::File or Msg::File to avoid clashes.

## Let me know how you get on, Trevor

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)
