在rails 3.2 mountable engine中,可以直接使用rake db:migrate,不过在rails 4.0.0.rc1中没用了,可以自己修正下,修改
activerecord-4.0.0.rc1/lib/active_record/railties
task :load_config do ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration || {} ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths end
修改为:
task :load_config do ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration || {} ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths if defined?(ENGINE_PATH) && engine = Rails::Engine.find(ENGINE_PATH) if engine.paths['db/migrate'].existent ActiveRecord::Migrator.migrations_paths += engine.paths['db/migrate'].to_a end end end
如果不修改rails源码,可以在engine的lib/tasks/*.task中添加
namespace :db do task :load_config do ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration || {} ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths if defined?(ENGINE_PATH) && engine = Rails::Engine.find(ENGINE_PATH) if engine.paths['db/migrate'].existent ActiveRecord::Migrator.migrations_paths += engine.paths['db/migrate'].to_a end end end end