DEV Community

Maya Sheriff
Maya Sheriff

Posted on

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 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
Enter fullscreen mode Exit fullscreen mode

Execute the generated migrations with:

rake db:migrate
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Adding and Removing columns

Ex:
rails g migration AddImageToPosts
    OR
rails g migration RemoveExpiresOnFromPosts
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay