Can I create a migration for the model?

I have already created a custom model with all the necessary properties.

Now i want to run

rails g forest users

but of course it will not work. b / c user model already exists.

Is it possible for me to create a migration for the current user model (create a value, create a script wrapping with the current model so that I can re-run it later).

That way, I can then destroy the model, start the scaffold, and then migrate with the old columns that I installed earlier.

perhaps?

+4
source share
2 answers

You can create the migration manually manually using rails generate migration . Here the output of this function helps:

 Usage: rails generate migration NAME [field:type field:type] [options] Options: -o, --orm=NAME # Orm to be invoked # Default: active_record Runtime options: -f, [--force] # Overwrite files that already exist -p, [--pretend] # Run but do not make any changes -q, [--quiet] # Supress status output -s, [--skip] # Skip files that already exist Description: Create rails files for migration generator. 
+3
source

This can happen if you are working with a model that is not supported by the database, then you decide that you want it to be, and expand ActiveRecord::Base and realize that there is no table. I would go into the Rails console and look at the methods in the class. You can print the setup methods as follows:

>> y Model.instance_methods(false).grep /\=/ # "Model" is the name of your class

Then I create a new migration, as usual, and define the columns with their types. for example, if your model has a column of type string name names, rails g migration Model name:string . You might want to delete some of the generated files. If it does not work because it matches the name, I would check if there is a force parameter, or I would temporarily rename my existing model class and then call it back after creating the migration.

0
source

Source: https://habr.com/ru/post/1341452/


All Articles