0
votes

I am trying to require a file located in lib (in my Rails app).

In development, this line works without any problems.

require "km"

However, in Heroku, I get a "no such file to load" error.

Via the Heroku console, I've tried a number of variants in terms of specifying paths to the file. For example:

require "lib/km"
require "./lib/km.rb"

And so forth. This is based on answers from these questions, which suggest that the issue is specifying the path to the file.

Rails, Heroku does not load my lib

Heroku console not loading file in /lib?

I've also tried adding this autoload line suggested here, which doesn't seem to solve it.

config.autoload_paths += Dir["#{config.root}/lib/**/"]

http://www.williambharding.com/blog/technology/rails-3-autoload-modules-and-classes-in-production/

How do I tell Rails to find the lib file? And why is there a difference in behaviour for require in development vs production?

UPDATE:

The issue turns out to be related to git. The original filename was "KM.rb", which was later changed to "km.rb". However, git didn't pick up the case change, so the change never got pushed onto the Heroku environment.

A key step was using the Ruby Dir commands in Heroku console to investigate the file structure and confirm the filename within the environment on Heroku.

1
Try using require_relative instead (if you're under Ruby 1.9).jdoe
@jdoe: Thanks for the tip. It turns out that the answer has to do with git. The file was originally checked in as "KM.rb" (since the class within it is "KM"). It was later changed to "km.rb" to follow lowercase convention. But git didn't pick up the case change. Your comment on require_relative sparked investigation, which led to stackoverflow.com/questions/4965556/…. From this, we saw we could use Dir to investigate file structure on Heroku in console, and that's when we realized that filename hadn't updated. Thank you.bigwinner

1 Answers

0
votes

I was getting the "no such file to load" error and found that I had the wrong case on a require statement

  require_relative './place/Places.rb'

when the actual filename was places.rb Worked fine on Windows since it's case insensitive