title: How Do I Create A Custom Rails Generate Model function?
body: <p>I have a Rails 5 application that has separate migrations for three databases. I'm adding a new database. I normally do a <code>rails g model field1:type1 field2:type2 field3:type</code>.... to create my database tables with the desired fields. I want to create generate model statements where it will generate the model for the desired database and put it in the corresponding db/migrate folder.</p>
  
  <p>I have custom database migration generators for the additional databases in lib/generators. Here is an example of the custom migration generator. </p>
  
  <pre><code>lib/generators/stats_migration_generator.rb
  
  require 'rails/generators/active_record/migration/migration_generator'
  
  class StatsMigrationGenerator &lt; ActiveRecord::Generators::MigrationGenerator
    source_root File.join(File.dirname(ActiveRecord::Generators::MigrationGenerator.instance_method(:create_migration_file).source_location.first), "templates")
  
    def create_migration_file
      set_local_assigns!
      validate_file_name!
      migration_template @migration_template, "db_stats/migrate/#{file_name}.rb"
    end
  end
  </code></pre>
  
  <p>I assume I need to create lib/generators/mydb_model_generator.rb or whatever the correct filename structure is for each additional database. I'm thinking I could do something like this after looking at the <a href="https://github.com/rails/rails/blob/master/activerecord/lib/rails/generators/active_record/model/model_generator.rb" rel="nofollow noreferrer">model_generator.rb</a> file in GitHub.</p>
  
  <pre><code>lib/generators/stats_model_generator.rb
  
  require 'rails/generators/active_record/model/model_generator'
  
  class StatsModelGenerator &lt; ActiveRecord::Generators::ModelGenerator
    source_root File.join(File.dirname(ActiveRecord::Generators::ModelGenerator.instance_method(:create_migration_file).source_location.first), "templates")
  
    def create_migration_file
      set_local_assigns!
      validate_file_name!
      migration_template @migration_template, "db_stats/migrate/#{file_name}.rb"
    end
  end
  </code></pre>
  
  <p>Here are my questions after looking at the git repository for Rails, <a href="http://guides.rubyonrails.org/generators.html#generator-methods" rel="nofollow noreferrer">the Rails Guide regarding creating and customizing Generators</a> and the Ruby on Rails API documentation:</p>
  
  <ol>
  <li>How do I find what I need to require?</li>
  <li>How do I find what method I need to override the db/migrate folder name?</li>
  <li>How do I find out how to name the rb file for the model generator?</li>
  </ol>
  
  <p>Are my assumptions correct in my attempt to create the model generator?</p>