Skip to content

Instantly share code, notes, and snippets.

@ddellacosta
Created August 11, 2011 06:29
Show Gist options
  • Save ddellacosta/1139025 to your computer and use it in GitHub Desktop.
Save ddellacosta/1139025 to your computer and use it in GitHub Desktop.
why aren't my models getting loaded in my app?
# in plugin_dir/lib/plugin_name.rb
...
%w{ models controllers }.each do |dir|
path = File.join(File.dirname(__FILE__), 'app', dir)
$LOAD_PATH << path
# it would be nice if someone updated the documentation.
ActiveSupport::Dependencies.autoload_paths << path
ActiveSupport::Dependencies.autoload_once_paths.delete(path)
end
# model (in plugin_dir/app/models/my_model.rb)
class MyModel < ActiveRecord::Base
# ...
def my_method
# does stuff
end
end
# if I have initializer (my_app/config/initializers/my_model.rb), it loads, and in the console I can see it
> MyModel
=> MyModel(id: integer ... )
# But if not:
> MyModel
NameError: uninitialized constant MyModel
# What am I doing wrong? Why aren't my models from my plugin loading without the initializer?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment