Forum: Ruby on Rails Module and Model

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.
D53151d3fb2de557729740292239d270?d=identicon&s=25 Thomas Kwan (thomask)
on 2006-04-19 05:19
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?
0ca6dacbed829354319d844346cbd5f3?d=identicon&s=25 Dan Sketcher (Guest)
on 2006-04-19 05:39
(Received via mailing list)
I might be way off here (i've only had to do that kind of thing once)
but try ::File.directory?

Cheers,
Dan
D53151d3fb2de557729740292239d270?d=identicon&s=25 Thomas Kwan (thomask)
on 2006-04-19 06:12
Hi Dan,

Thanks for the suggestion. Unfortunately, same error.
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2006-04-19 07:11
(Received via mailing list)
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 Squires
http://somethinglearned.com
D53151d3fb2de557729740292239d270?d=identicon&s=25 Thomas Kwan (thomask)
on 2006-04-19 08:05
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.
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2006-04-19 16:04
(Received via mailing list)
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
D53151d3fb2de557729740292239d270?d=identicon&s=25 Thomas Kwan (thomask)
on 2006-04-19 16:37
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
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2006-04-19 18:11
(Received via mailing list)
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 Squires
http://somethinglearned.com
D53151d3fb2de557729740292239d270?d=identicon&s=25 Thomas Kwan (thomask)
on 2006-04-19 18:19
Trevor,

Yes, that works.That was also suggested by Dan. It wasnt working when I
tried this earlier on. Thanks to both of you!
This topic is locked and can not be replied to.