Rails load path questions

Posted by HelpMe on Stack Overflow See other posts from Stack Overflow or by HelpMe
Published on 2011-01-07T16:50:33Z Indexed on 2011/01/07 16:54 UTC
Read the original article Hit count: 213

Filed under:

Say I have some custom classes that don't belong in models, controllers etc, I should put this in /lib correct?

In rails < 3 I would add this directory to my loadpath and in rails 3+ I would add this to my autoload_path. Is this correct?

Now say I have some classes that extends already defined classes. Where should I put this so its run on startup. Forexample say I want to add the method 'foo' on String.

class String
  def foo
    'foo;
  end
end

Where should I put this class so it's defined on startup?

Another weird bug I have is when I try to namespace classes in lib.

module MyProject
 class Foo
 end
end

Now in a console:

ruby-1.9.2-p136 :004 > Bags::Foo
LoadError: Expected /Users/me/workspace/my_project/lib/foo.rb to define Foo
 from /Users/rob/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:492:in `load_missing_constant'
 from /Users/rob/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:183:in `block in const_missing'

I keep getting this error. How can I load this file?

Thanks in advance for any help!

© Stack Overflow or respective owner

Related posts about ruby-on-rails