I’m trying to break several pieces of reusable application functionality into Rails engines. I have no trouble at all getting one engine to work, but the app doesn’t seem to load data from subsequent engines. I’m developing multiple engines simultaneously, so I’m requiring them into my testapp’s gemfile with the :path option.
Here’s my (simplified) setup (my understanding of the bare minimum to setup an engine with a simple model):
my_engines/engine1/lib/engine1.rb:
module Engine1
require 'engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3
end
my_engines/engine1/lib/engine.rb:
require 'engine1'
require 'rails'
module Engine1
class Engine < Rails::Engine
end
end
my_engines/engine1/app/models/engine1/model1.rb:
module Engine1
class Model1
end
end
My second Engine, engine2 is setup identically and in the same parent directory (just with the name engine2 and the model model2).
I’m using jeweler to package and generate gemspecs for both engines, and I’m requiring both engines in a test application like so:
my_engines/testapp/Gemfile:
gem 'engine1', :path => '../engine1'
gem 'engine2', :path => '../engine2'
The weird thing is that when I fire up rails console for my testapp, Engine1::Model1.new works, but Engine1::Model1.new results in “NameError: uninitialized constant Engine2::Model2”. This seems to be true for all models, routes, controllers, etc. that I include into any subsequent engines. I’ve scoured the internet to no avail. Any thoughts?
I think I’ve figured this out. Just in case anyone else out there happens to be playing with Rails3 engines (much recommended), and runs into this issue (hopefully not),
the problem was having my engine.rb file and my engine_name.rb files
both sitting side-by-side in the lib dir. The solution is to create
an engine_name dir within lib and put your engine.rb file in there (I
guess Rails only loads the first engine.rb file it finds in a gem or
plugin’s lib dir). So…
Bad (only loads one engine.rb in host app):
Seems to work: