rails - help trying to use models in sub-folders

ruby, ruby-on-rails

Solution

Rails is automagically able to find correctly-namespaced classes in subfolders of app/models without your having to do anything special. Since you've added subfolders of app/models to the autoload path, Rails now expects to find non-namespaced classes in those locations.

If you delete the autoload path change and define your class as Mohanraj suggested:

# app/models/system/admin_user.rb
module System
  class AdminUser
    # your code here
    # ...
  end
end

then you shouldn't have any problems.

Problem

I'm using Rails 4 and ActiveAdmin with SQLite. 1. I've generated those models and moved files into folders: ``` app models system - admin_user.rb - customer.rb resources - document.rb ``` 2. Added to config/application.rb: ``` config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**/}')] ``` 3. In the model files I just added prefix to model name: ``` class System::AdminUser < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable end ``` -- So, when I try to run `rake db:migrate` I get this error: ``` LoadError: Unable to autoload constant AdminUser, expected /Users/john/testing/app/models/System/admin_user.rb to define it ``` Am I doing something wrong?

Original source