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
en
I know this is a old question but I've just faced the same problem and after some searches, I found a solution so I think it worth to share it.
I wanted to use a model "Foo" in one required files located in my /lib directory. First, I did this and it didn't work :
# in my rake file
task :foo_task do
require /some_path/lib/bar.rb
end
# in /lib/bar.rb
puts "Foo = #{Foo.count} "
# => uninitialized constant Foo
After some searches, I found that to access models in my lib's files, I need to specify the environment in my task. So I just added this to my task declaration :
task :foo_task => [:environment] do
Now, when I call my task, it correctly puts the number of Foo :
# => Foo = 6