rails g shortcuts
agandaur-ii
Posted on March 24, 2020
Been living in the rails world for awhile now and I am learning to appreciate all of the short cuts it provides you. Since I have been creating a lot of new apps from scratch to practice, I am especially appreciating the generate shortcuts that you can perform when setting everything up. So I wanted to create a short list of the generator shortcuts that I find to be most beneficial for me, and hopefully for you too!
The best part is that for any generation instead of writing:
rails generate [thing to generate]
You can just write:
rails g [thing to generate]
Neat!
The next part of building your app is, most likely, setting up database. Once you go through all of the steps of figuring out what you want on all those tables, you can get started on creating those files. Without the generator, you would go into your db/migrate folder, create a file for your table, set up the class (making sure it inherits from ActiveRecord), write the change method including create_table
and all the following column names, etc., etc...
That was a lot just to write, let alone code! Thankfully, rails g migration will take care of all of that for us. Let's say we are creating a flight app of some kind. We'll need planes for that, so if we wanted to create a table for our planes all we would need would be something like the following:
rails g migration CreatePlane model:string callsign:string
which will produce:
class CreatePlane < ActiveRecord::Migration[6.0]
def change
create_table :planes do |t|
t.string :model
t.string :callsign
end
end
end
We can simplify even more but writing:
rails g migration CreatePlane model callsign
as rails will automatically assumes that the column type will be a string if we do not add a type to it ourselves. Pretty cool that ones line of code can do so much for us! The "Create" keyword is imperative, as it tells rails we are going to be making a new table. It then assumes the text following that will be the column names.
We might create an airport table next. Our planes will need to go somewhere, right? Just one more easy line of code like the above and we have another table ready to go. Now let's associate them through flights. As long as we have our foreign keys on the flight table from airplanes and airports, we'll be golden. Now, to do that, we could use a similar line like the above, but rails can do one better! It has specific syntax for when setting up a join table, which looks like this in this example:
rails g migration CreateFlight number airplane:references
airport:references
Which would create this:
class CreateFlight < ActiveRecord::Migration[6.0]
def change
create_table :flights do |t|
t.string :number
t.references :airplane, foreign_key: true
t.references :airport, foreign_key: true
end
end
end
Now with just a few lines of code, we have created 3 tables saving us time and ensuring we avoid simple bugs we may find if we tried typing this out ourselves. Thanks rails! We can also use the "Add" and "Remove" keywords to help quickly write migration files to add and remove columns where necessary.
Next we might want to add our models or our controllers. Rails has a generator for both of those! Just rails g model [model name]
and rails g controller [controller name]
. Now we could do these two separate commands for our airplanes, airports, and flight, or we could use a different shortcut: rails g resource [resource name]
! With the simple line of rails g resource Airplane
, rails will give us an airplane model, controller, view folder, and routes! Even more time saved and potential bugs avoided thanks to that good ol' rails magic.
There are more generators out there, but these are the ones I find myself using the most. To read more about rails generate, checkout the documentation here: https://guides.rubyonrails.org/command_line.html#rails-generate
Posted on March 24, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.