Accessing models from within the lib directory in a Rails 3 project
ruby-on-rails
Solution
The solution was, that my file in /lib was actually being required by a rake file, and it seems rake files are loaded before the whole auto-load system is setup by Rails, so it couldn't find the model. After I remove the require from the .rake file, everything started working.
Problem
I have a file in the lib directory that uses some constants defined in a model, like: ``` class User < ActiveRecord::Base MAX_EMAIL_ADDRESS_LENGTH = 255 end ``` and then I have in lib/foo.rb ``` module Foo LONG_EMAIL_ADDRESS = "foo@bar.com".rjust(User::MAX_EMAIL_ADDRESS_LENGTH, "a") end ``` It fails due to not finding the class User. How can I load User before that file on lib? I'm loading that file by having this in my application.rb: ``` config.autoload_paths += %W(#{config.root}/lib) ```