Rails Generator CLI — 101
Losing too much time on your Coding Challenge creating your model, migrations, views and more??
It’s time to learn about a tool that helps you save some time and effort with your code. After all, aren’t we programmers lazy?? Heck Yes!!! so, let’s use the tools we have and code fast!!.
1. Model Generators
To create a model, follow this formula:
rails g model NameOfModel column_name:datatype column_name2:datatype2 --no-test-framework
For example:
rails g model Dog name:string breed:string bio:text --no-test-framework
(yes, another pet’s example! sorry not sorry)
will generate this file:
# db/migrate/20180701032833_create_dogs.rbclass CreateDogs < ActiveRecord::Migration[5.1]
def change
create_table :dogs do |t|
t.string :name
t.string :breed
t.text :bio t.timestamps
end
end
end
2. Migration Generators
Adding Columns
To add an additional column to a table, follow this formula:
rails g migration add_name_of_column_to_table_name name_of_column:datatype --no-test-framework
For example, if we want to add the year a dog was born, we would type in the following:
rails g migration add_year_born_to_dogs year_born:integer --no-test-framework
which will produce the following file:
# db/migrate/YYYYMMDDHHMMSS_add_year_born_to_dogs.rbclass AddYearBornToDogs< ActiveRecord::Migration[5.1]
def change
add_column :authors, :year_born, :integer
end
end
Adding multiple columns
We can also add multiple columns to the dogs
table in a single CLI command like so:
rails g migration add_barks_to_dogs barks_done:integer last_game_played:string --no-test-framework
resulting in:
# db/migrate/YYYYMMDDHHMMSS_add_barks_to_dogs.rbclass AddBarksToDogs< ActiveRecord::Migration[5.1]
def change
add_column :dogs, :barks_done, :integer
add_column :dogs, :last_game_played, :string
end
end
Removing Columns
To remove a column, i.e. the last_game_played
column from dogs
, we can type in CLI:
rails g migration remove_last_game_played_from_dogs last_game_played:string --no-test-framework
Which creates:
#db/migrate/YYYYMMDDHHMMSS_remove_last_game_played_from_dogs.rbclass RemoveLastGamePlayedFromDogs < ActiveRecord::Migration[5.1]
def change
remove_column :dogs, :last_game_played, :string
end
end
3. Resource Generators
To create a new Model, corresponding database table, controller, and an empty views folder type in:
rails g resource ModelName column_name:datatype column_name2:datatype2 --no-test-framework
For example, if I want to create a Dog_walker
model with a corresponding name that a dog will belong to:
rails g resource DogWalker first_name:string dog_id:integer --no-test-framework
4. Datatypes
Thanks to this great stack overflow post, below are the list of almost all the datatypes that Rails supports.
:string
is for small data types such as a title. It’s the default type pf date.
:text
is for longer pieces of textual data, i.e. paragraphs
:integer
for whole numbers
:decimal
for decimals
:datetime
store the date and time into a column (handles timezone)
:timestamp
for storing date and time into a column (timezone independent)
:binary
is for storing data such as images, audio, or movies
:primary_key
unique key that can uniquely identify each row in a table
:float
also for decimals.
:boolean
is for storing true or false values
:date
store only the date
5. Don’t forget to rake:db migrate
After using your generator don’t forget to type on your terminal rake:db migrate.
generate what??
6. Resources
Still have more questions?. Here are some more in-depth resources if any additional information we might need for successfully using our generators.
Some of the information used for this blog was grabbed from the previous websites and different medium publications.