Rails Migration Tips and Examples (not a full step by step)

Generates the model and migration, but also all of the RCAVs for index, show, create, update, and destroy.

The model name must be singular.
Separate column names and datatypes with colons (NO SPACES).
Separate name:datatype pairs with spaces (NO COMM…


This content originally appeared on DEV Community and was authored by Maya Sheriff

Generates the model and migration, but also all of the RCAVs for index, show, create, update, and destroy.

  • The model name must be singular.
  • Separate column names and datatypes with colons (NO SPACES).
  • Separate name:datatype pairs with spaces (NO COMMAS).
rails generate draft:resource <MODEL_NAME_SINGULAR> <COLUMN_1_NAME>:<COLUMN_1_DATATYPE> <COLUMN_2_NAME>:<COLUMN_2_DATATYPE> # etc

EX:
rails generate draft:resource board name:string
        OR
rails generate draft:resource post title:string body:text expires_on:date board_id:integer

Execute the generated migrations with:

rake db:migrate

This will remove all the files that rails generate draft:resource post ... had previously added. You can then correct your error and re-generate from scratch.

rails destroy draft:resource post

If you made a mistake when generating your draft:resource AND you’ve already run rake db:migrate, then first you have to run the command:

rake db:rollback

Adding and Removing columns

Ex:
rails g migration AddImageToPosts
    OR
rails g migration RemoveExpiresOnFromPosts
  • If your database gets into a weird state (usually caused by deleting old migration files), your ultimate last resort is
  • This will destroy your entire database and all the data within it. Then, you can fix your migrations and re-run all from scratch with rake db:migrate, then rake sample_data to recover the sample data.
rake db:drop


This content originally appeared on DEV Community and was authored by Maya Sheriff


Print Share Comment Cite Upload Translate Updates
APA

Maya Sheriff | Sciencx (2024-06-26T16:21:43+00:00) Rails Migration Tips and Examples (not a full step by step). Retrieved from https://www.scien.cx/2024/06/26/rails-migration-tips-and-examples-not-a-full-step-by-step/

MLA
" » Rails Migration Tips and Examples (not a full step by step)." Maya Sheriff | Sciencx - Wednesday June 26, 2024, https://www.scien.cx/2024/06/26/rails-migration-tips-and-examples-not-a-full-step-by-step/
HARVARD
Maya Sheriff | Sciencx Wednesday June 26, 2024 » Rails Migration Tips and Examples (not a full step by step)., viewed ,<https://www.scien.cx/2024/06/26/rails-migration-tips-and-examples-not-a-full-step-by-step/>
VANCOUVER
Maya Sheriff | Sciencx - » Rails Migration Tips and Examples (not a full step by step). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/26/rails-migration-tips-and-examples-not-a-full-step-by-step/
CHICAGO
" » Rails Migration Tips and Examples (not a full step by step)." Maya Sheriff | Sciencx - Accessed . https://www.scien.cx/2024/06/26/rails-migration-tips-and-examples-not-a-full-step-by-step/
IEEE
" » Rails Migration Tips and Examples (not a full step by step)." Maya Sheriff | Sciencx [Online]. Available: https://www.scien.cx/2024/06/26/rails-migration-tips-and-examples-not-a-full-step-by-step/. [Accessed: ]
rf:citation
» Rails Migration Tips and Examples (not a full step by step) | Maya Sheriff | Sciencx | https://www.scien.cx/2024/06/26/rails-migration-tips-and-examples-not-a-full-step-by-step/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.