Module and Model


#1

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 methoddirectory?’ 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?


#2

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

Cheers,
Dan


#3

Hi Dan,

Thanks for the suggestion. Unfortunately, same error.


#4

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


#5

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


#6

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.


#7

Trevor,

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

  • Create app/models/msg/file.rb

    class Msg::File < ActiveRecord::Base
    set_table_name “msg_files”
    end

  • Create app/lib/msg/my_helper.rb

    require ‘msg/file’
    module Msg
    class MyHelper

    def initialize()
    end
    
    def reproduce_error
       Msg::File.new
       File.directory? 'tmp'
    end
    

    end
    end

  • Create app/script/driver.rb

    require ‘msg/my_helper’

    helper = Msg::MyHelper.new
    helper.reproduce_error

Thanks


#8

Trevor,

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


#9

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