Ruby on Rails Engine - How To Keep Your Engine Migrations Abstracted From Your Host Rails App

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?
Software Development
by Jey Geethan | January 16, 2019

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!


Related Articles

Building Microservices For Quick Wins
Software Development

Building Microservices For Quick Wins

by Jey Geethan | October 03, 2018
Microservices have always been a hot topic among software firms. Does your firm really need a microservice? Read on to find out.

Top 5 Tips for Software Development Managers To Conduct Effective 1 on 1s With Your Reports
Software Development

Top 5 Tips for Software Development Managers To Conduct Effective 1 on 1s With Your Reports

by Jey Geethan | October 05, 2020
Some of the effective tips that can enhance your 1 on 1s with your reports. In 2020, software development is one of the major industries which provides millions of jobs. In fact, if you look at the statistics, this industry is poised to grow even further. According to Slashdata, it's expected to reach 45 million developers by 2030. As a manager, it's your responsibility that 1 on 1s are effective and help your reports. I want to talk about a few of the things that make this process really effective.