Rails 2.3.5: How does one access code inside of lib/directory/file.rb?

Posted by randombits on Stack Overflow See other posts from Stack Overflow or by randombits
Published on 2010-05-08T23:22:52Z Indexed on 2010/05/08 23:28 UTC
Read the original article Hit count: 160

Filed under:
|

I created a file so I can share a method amongst many models in lib/foo/bar_woo.rb. Inside of bar_woo.rb I defined the following:

module BarWoo
  def hello
   puts "hello"
  end
end

Then in my model I'm doing something like:

  def MyModel < ActiveRecord::Base
      include Foo::BarWoo
      def some_method
         Foo::BarWoo.hello
      end
  end

The interpreter is complaining that it expected bar_woo.rb to define Foo::BarWoo.

The Agile Web Development with Rails book states that if files contain classes or modules and the files are named using the lowercase form of the class or module name, then Rails will load the file automatically. I didn't require it because of this.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby