Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to add migration for rails app to ruby gem?

For my rails project I want to write a ruby gem which have an ActiveRecord model eg. Animal < ActiveRecord::Base. Is it possible to add migration cretae_animals to the gem in such a way that when the gem installed in my app and I run rake db:migrate that migration will execute?

like image 965
lx00st Avatar asked Oct 20 '22 18:10

lx00st


1 Answers

You can do this by including the migrations with your gem, as well as including a rake task that runs them. Then you call the rake task as follows:

myGem = Gem::Specification.find_by_name 'gem-name'
load "#{myGem.gem_dir}/lib/tasks/my_migration.rake"

(Proper credit to Andy Atkinson, where I originally learned this for a similar project.)

like image 107
taintedzodiac Avatar answered Nov 02 '22 03:11

taintedzodiac