Reputation: 2501
I am using the Monologue Gem and Devise Gem.
I am using config autoload_paths to load my subdirectories which include some presentation models.
Rails 4: organize rails models in sub path without namespacing models?
app/config/application.rb
config.autoload_paths += Dir[Rails.root.join('app', 'models', '{*/}')]
My issue, I believe, stems from when I needed to override the Monologue User model. To do so I created a local file
app/models/monologue/user.rb
class Monologue::User < ActiveRecord::Base
# code
end
I also have my app's user model at
app/models/user.rb
class User < ActiveRecord::Base
# code
end
The error that I am recieving is
ruby-2.1.5/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:481:in `load_missing_constant': Unable to autoload constant User, expected /Users/Shared/code/kindrdfood/RecRm/app/models/monologue/user.rb to define it (LoadError)
Upvotes: 0
Views: 866
Reputation: 2518
You've included a part of the classes namespace into the autoload path (app/models/monologue
).
Just keep the autoloading path as it is. The path app/models
is already included. Rails tries to find an appropriate file to include for a given class name if it does not exist yet. Without your modification to the load path, User
should autoload app/models/user.rb
and Monologue::User
should autoload app/models/monologue/user.rb
.
What Rails actually does, is calling the underscore
method on a your class (respectively its string representation). So on a Rails console, you could do something like this:
>> "Monologue::User".underscore
=> "monologue/user"
>> "User".underscore
=> "user"
Edit:
If you want to add custom load pathes to rails' autoload feature, I'd recommend to not put them inside of a folder which is already included in the list. Maybe something like this:
config.autoload_paths << File.join(config.root, "app/decorators")
config.autoload_paths << File.join(config.root, "app/workers")
config.autoload_paths << File.join(config.root, "lib")
Upvotes: 1