Have you ever worked on building a Rails Engine and wanted to keep the models, the migrations and everything inside the engine rather than using a generator to copy paste them into your host Rails App? That's a problem everybody faces one time or the other when building Rails Engines to abstract out your huge Rails App. I have found one such solutions that can be helpful for you to keep them separate. Here is a solution for it.
Problem Statement:
Instead of using
rails g my_engine:install
to copy paste the migrations from engine to your rails app, you want to just keep the migrations inside the Rails Engine and do not bother about it.
Solution:
Add the following lines to your engine, so that your engine's migration files are added to the rails app as well.
module MyEngine
class Engine < ::Rails::Engine
isolate_namespace MyEngine
initializer :append_migrations do |app|
unless app.root.to_s.match root.to_s
config.paths["db/migrate"].expanded.each do |expanded_path|
app.config.paths["db/migrate"] << expanded_path
end
end
end
end
end
Now you can do the following from your rails app to run your migrations as you always do:
rake db:migrate
Hope it helps you! If it does help you, give me a shoutout below!


![Cover image for [.NET] ToolTip and "Cannot access a disposed object" exception by Jey Geethan](https://www.jeygeethan.com/rails/active_storage/representations/redirect/eyJfcmFpbHMiOnsiZGF0YSI6MTEwLCJwdXIiOiJibG9iX2lkIn19--3b4c315fc9a79786cd0ea4b85fdd37798889788d/eyJfcmFpbHMiOnsiZGF0YSI6eyJmb3JtYXQiOiJwbmciLCJyZXNpemVfdG9fZml0IjpbODAwLDgwMF19LCJwdXIiOiJ2YXJpYXRpb24ifX0=--e6e6a0f38c858cc47eaf7fc395a39df3afd3bb47/9KBMU.png)
![Cover image for [VB.NET] Finding whether its Design Mode or Runtime Mode for Forms by Jey Geethan](https://www.jeygeethan.com/rails/active_storage/representations/redirect/eyJfcmFpbHMiOnsiZGF0YSI6MTA4LCJwdXIiOiJibG9iX2lkIn19--02a207fb21e62c2d52791c4a53fc2ce9e251679f/eyJfcmFpbHMiOnsiZGF0YSI6eyJmb3JtYXQiOiJqcGVnIiwicmVzaXplX3RvX2ZpdCI6WzgwMCw4MDBdfSwicHVyIjoidmFyaWF0aW9uIn19--cfb9b5ee4affce3d9631a47d315e7e009392c6a2/DesignTime.jpeg)