Reputation: 1244
Before you marked this questions as duplicated, I've checked this other very related questions: 1, 2, 3, 4 and 5, and basically, all of them is the same way to load the lib
modules, but User
still keeps throwing an error.
models/user.rb
# This line throws an error:
# cannot load such file -- my-project-root/lib/model_with_properties.rb
require "#{Rails.root}/lib/model_with_properties.rb"
class User < ActiveRecord::Base
# This line throws an error:
# uninitialized constant User::ModelWithProperties
include ModelWithProperties
...
end
lib/model_with_properties.rb
module ModelWithProperties
# Some functions
end
config/application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
require 'yaml'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Project
class Application < Rails::Application
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
# Load the lib folder, a thousand times
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
config.autoload_paths << "#{Rails.root}/lib"
end
end
What am I missing?
Another useful information:
julian$ rails r 'puts ActiveSupport::Dependencies.autoload_paths'
Running via Spring preloader in process 38445
/my-project-root/lib
/my-project-root/lib/
/my-project-root/lib/assets/
/my-project-root/lib/tasks/
/my-project-root/app/assets
/my-project-root/app/controllers
/my-project-root/app/helpers
/my-project-root/app/jobs
/my-project-root/app/mailers
/my-project-root/app/models
/my-project-root/app/controllers/concerns
/my-project-root/app/models/concerns
/my-project-root/test/mailers/previews
julian$ rails -v
Rails 4.2.3
Answer
Big thanks to @jvillian, I was using RubyMine and I assumed the file extensions were hidden, and all that matter was the Ruby Icon on the IDE. Actually the .rb
extension was missing.
Upvotes: 0
Views: 1981
Reputation: 20263
In console, do Dir["lib/**/*.rb"]
to make sure the file is actually there. Once you fix that, try removing the require
statement in user.rb
(as @Stefan suggests). Not altogether necessary, but require
statements can sometimes mess with AutoLoading.
Upvotes: 1