TransWikia.com

Is it possible to override the create_migration template in Rails 6?

Stack Overflow Asked by Iván Quiñones on December 1, 2021

I tried placing my custom file in multiple locations inside the lib folder with no success.

I found a PR from 2017 to reactivate this behavior but wasn’t merged.
https://github.com/rails/rails/pull/13972

I had a look at
https://github.com/rails/rails/blob/master/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb#L17

and it looks like it’s not implemented, but I don’t know if just this file should change.

I´ve found resources on how to do it in older rails versions, but is there any way to override the Rails templates in Rails 6?

2 Answers

Put your templates here:

<myproject>/lib/templates/active_record/migration/create_table_migration.rb.tt
<myproject>/lib/templates/active_record/migration/migration.rb.tt

See: https://github.com/rails/rails/pull/13972 for related discussion

Answered by kkurian on December 1, 2021

From this line of the documentation:

Since we want to customize Rails::Generators::HelperGenerator, we can do that by simply making a template copy inside lib/templates/rails/helper with the name helper.rb.

In the case for migrations, the template names should be specified as follows (here are the default templates for reference):

  • lib/templates/active_record/migration/create_table_migration.rb.tt
  • lib/templates/active_record/migration/migration.rb.tt

Answered by nfisher on December 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP