views:

13

answers:

1

I have two models User and Manager. I use STI to inherit Manager from User model.

app/models/user.rb

class User < ActiveRecord::Base

end

custom_lib/models/manager.rb

class Manager < User

end

I have added the custom models to load path as follows:

config/environment.rb

config.autoload_path += File.join(RAILS_ROOT, "custom_lib", "models")

Every thing works as expected in development mode. In the production mode I get the following error:

The single-table inheritance mechanism failed to locate the subclass: Manager

For some reason rails is not loading the inherited classes.

To work around this issue I explicitly require the classes in an initializer.

config/initializers/custom_models.rb

Dir[File.join(RAILS_ROOT, "custom_lib", "models", "*.rb")].each do |file_name|
  require(File.join(File.dirname(file_name), File.basename(file_name, ".rb")))
end

I prefer to use autoload_path. I am wondering if anybody else has seen this behavior.

I am on Ruby 1.8.7, Rails 2.3.9, Ubuntu

Edit 1

I am aware that everything works if all the models are residing in app/models directory. In my application generated models are residing in custom location, hence this requirement.

A: 

Keep your manager.rb file in your app/models directory where your user.rb file is. That's where rails looks for models. There's no benefit to hiding the manager model in a different directory.

Once you do this, you can also get rid of mucking with the load path.

Jaime Bellmyer
This is not a relevant answer. My application is loading certain third party models. I can't dictate their location.
KandadaBoggu