Rails Quick & Dirty Tips: Useful Commands
Having just completed my Rails project for the Flatiron School, I feel like I’ve grown so much as a developer! I will never look at a URL in the same way again and I look forward to building on top of my newly acquired skills through future projects.
While challenging, this project was a fun way to help solidify some core concepts regarding model associations, nested routes, and form building.
For my project, I created a web application that allows dog owners to connect with one another through an events listing platform. Dog owners can add a profile for their furry companions, create events as a host, join events, and keep track of their upcoming events via a user platform.
Since my project evolved and morphed throughout the development phase, I found myself constantly referring back to the same rails commands, which much to my chagrin were scattered across the internet.
I compiled a reference guide which includes helpful rails commands that I used during my project. Anyone who has spent time in the world of rails knows how important syntax is to the programming language as well as the magic that can happen with a few keystrokes!
Rails Console Sandbox
The rails console sandbox is a great way to test out code without changing any data. Any modifications you make are rollbacked when you exit the sandbox session. The sandbox was especially helpful during the early stages of development, especially when I was testing out my model associations.
- To initiate, enter
rails console --sandbox
in your terminal
View Your Routes
One of the most important aspects of a rails project is defining routes. There are two conventional options to view the paths defined in your application .
- Open rails server in your terminal with command rails — s
- In your browser head to
localhost:3000/rails/info/routes
- Alternatively, you can enter
rails routes
in your terminal
Personally, I found the first option to be the most useful, as you can easily all your defined routes on a nicely formatted webpage.
Useful Rails Migrations
Add Column
Scenario: add breed column with attribute type of string to a pet table
- Step 1:
bin/rails g migration AddBreedToPet breed:string
- Step 2: Review the migration generation and then run your migration
rails db:migrate
Remove a Column
Scenario: remove color column with attribute type of string from the pet table
- Step 1: Bin/Rails g migration RemoveColorFromPet color:string
bin/rails g migration RemoveColorFromPet color:string
- Step 2: Review your migration and then run your migration Step 2: Review your migration and then run your migration
rails db:migrate
Rename a Column
Scenario: Rename temperament column in pet table to personality
- Step 1: Generate a new migration that is named accordingly
bin/rails g migration RenamePetTemperamentToPersonality
- Step 2: You will need to manually modify the migration inside the change method in the following way
rename_column :table_name, :old_column, :new_column
i.e. rename_column :pets :temperament, :personality
- Step 3: Run your migration
rails db:migrate
Change Column Attribute Type
Scenario: Change the weight column in the pet table from an attribute type of string to integer
- Step 1: Generate a new migration that is named accordingly
bin/rails g migration change_weight_to_be_integer_in_pets
- Step 2: You will also need to manually modify this type of migration, so open the migration file and in the change method edit the attribute type to the following:
change_column :table_name, :column_name, :new_type
i.e. change_column :pets, :weight, :integer
- Step 3: Run your migration
rails db:migrate
More Info