6

I want to override the default model file that's generated with rails generate model. I've created a template based on this file, but I can't figure out where to put it. Other answers seem to suggest /lib/templates/rails/model/model.rb or /lib/templates/rails/model/model_generator.rb, but neither of those do anything - I put the template in that location but when I run rails generate model ModelName it gets ignored.

Am I going about this the right way? Where should I put the template?

GMA
  • 5,816
  • 6
  • 51
  • 80

2 Answers2

16

Solved: I wanted lib/templates/active_record/model/model.rb.

GMA
  • 5,816
  • 6
  • 51
  • 80
  • 4
    I just had to look up my own old post again to make sure I had the right place. Note to Googlers: if you put your template in the above location and it doesn't seem to be working, make sure you've restarted Spring before running `rails g`. – GMA Nov 03 '14 at 05:18
  • 1
    or you can also do: `DISABLE_SPRING=true rails g ...` – gtournie Mar 17 '17 at 00:50
2

You probably need to put it in "lib/rails/generators/active_record/model/templates/model.rb". Here's the rails' default one: https://github.com/rails/rails/blob/master/activerecord/lib/rails/generators/active_record/model/templates/model.rb

Cristian Bica
  • 4,067
  • 27
  • 28
  • Does not look right. The [correct location is](https://github.com/rails/rails/blob/e6b286f9baca7c6c97c90288acdd563c40d5103f/activerecord/lib/rails/generators/active_record/model/model_generator.rb#L28) `lib/templates/active_record/model/model.rb.tt`, notice the `.tt` at the end, although this is not required. If overwritten, then pay attention to [the original template](https://github.com/rails/rails/blob/e6b286f9baca7c6c97c90288acdd563c40d5103f/activerecord/lib/rails/generators/active_record/model/templates/model.rb.tt). – viktorianer Dec 06 '21 at 16:25