Easier Ruby with Rails Generators
By: John Loberger
Tags: Web Development, Ruby on Rails, Software Development, Rails, Programming

When it comes to utilizing any of the many Ruby on Rail’s handy-dandy generators, a little can go a long way. This blog post will detail when, what, and how to use common Rail’s generators. To get started, open up a new Rail’s project and after running ‘bundle install,’ type the following into the terminal:

rails g

This will output a list of commands into your terminal, and sort of serve as a blueprint for how to proceed. The ‘rails g’ output is listed below.

Please choose a generator below. Rails: assets channel controller generator helper integration_test jbuilder job mailer migration model resource scaffold scaffold_controller task

Wow! With so many generator options, where do we go from here?

The best methodology behind choosing any certain generator should pertain to what is already contained in the project files themselves. If all you need is a single new Model or controller, then there is no need to use ‘rails g resource’ which will generate both of these files and more. Too much clutter can undoubtedly cause errors down the line, so keep the DRY principle in mind before running any generator.

Let’s start with the sole generator that should definitely be avoided; unless your goal is to produce an extremely fast proof of concept, or maybe just to flex the power of Ruby on Rails. When it comes to overpowered Rail’s generators, ‘rails g scaffold’ takes the cake. This generator will flesh out entire Ruby on Rails web application complete with Models, controllers, views, routes, along with several other often unnecessary files. At first glance this sounds amazing, almost like a harmless shortcut.

However, I urge you to not be fooled. For almost every application, ‘rails g scaffold’ should be used sparingly because it leaves little control to the developer and creates a plethora of code which will only bog down your project. Still, if you are in need of producing a few Models, controllers, and view directories from scratch; then I highly recommend ‘rails g resource.’

The rail’s generator ‘resource’ is essentially a generator which is comprised of four other generators acting in succession. To get a better idea of what ‘rails g resource’ is doing behind the scenes, let’s break down some of its individual parts. For example, if we were to run ‘rails g model’ we would use the following formula:

rails g model ModelName column_name:datatype column_name2:datatype2 --no-test-framework

Perhaps we wish to generate a Model for an Author class, this would look like:

rails g model Author name:string genre:string --no-test-framework

Not only would this command generate an ‘Author.rb’ file in our Models folder, but it will also create the following migration file:

# db/migrate/20150301576893_create_authors.rb

class CreateAuthors < ActiveRecord::Migration[5.1]

def change

create_table :authors do |t|

t.string :name

t.string :genre

t.timestamps

end

end

end

As a side note, tagging ‘ — no-test-framework’ to the end of our generate command is simply telling Rails to not generate any rspec/test files. There are certain situations where these might come in handy but for the most part they are unnecessary code blocks and therefore best to leave out.

Following up with the Model generation command, you can also generate a sole migration file without creating a new Model class using this formula:

rails g migration add_column_name_to_table_name column_name:datatype --no-test-framework

Say we wanted to add a bio to our author table, this would look like:

rails g migration add_bio_to_authors bio:text --no-test-framework

After running this command the following migration file would be created:

# db/migrate/20120301776896_add_bio_to_authors.rb

class AddBioToAuthors < ActiveRecord::Migration[5.1]

def change

add_column :authors, :bio, :text

end

end

This framework works the same for adding multiple columns as well as deleting columns. If we wanted to add multiple columns the formula is:

rails g migration add_more_to_authors first_publication_date:integer number_of_books_sold:integer --no-test-framework

Conversely, deleting a column is as simple as the following:

rails g migration remove_number_of_books_sold_from_authors number_of_books_sold:integer --no-test-framework

This brings us to ‘rails g resource’ which will create a new Model, database migration file, controller, and an empty views folder. This command follows the same formula as the model generator:

rails g resource ModelName column_name:datatype --no-test-framework

The resource generator should generally be used when there are no controller, migrations, or models created yet. Essentially, you should use ‘rails g resource’ when you are starting a new Rails app from scratch. It gives you room to control your code without creating too many unnecessary files. Please read more about Rails generators here.