DEV Community

emmacohanim
emmacohanim

Posted on

Rails Generators

Introduction

When building a rails application, we, as developers, must build the structure of our database as well as actions that allow the client to interact with our database and retrieve our data for the user. This requires us to create migrations, models, controllers, and routes. We can accomplish these tasks one of two ways: manually or using rails resource. Read on to learn about both of these methods and why developers generally prefer to use the second method.

Manual Method

In order to build our rails application, we first must create our migration. We can do so by cding into our application and entering the following in the terminal:

rails g migration manual_method
Enter fullscreen mode Exit fullscreen mode

Once we have our migration, we must build our table by specifying our attribute names and data types.

class ManualMethod < ActiveRecord::Migration[7.0]
  def change
    create_table :manual_methods do |t|
      t.string :attribute_1
      t.integer :attribute_2

  end
end

Enter fullscreen mode Exit fullscreen mode

Now that we have structured our table, we need to create the schema for our database. We can do so by migrating our new migration.

rails db:migrate
Enter fullscreen mode Exit fullscreen mode

With our table created, we can turn our attention to creating our model.

rails g model manual_method
Enter fullscreen mode Exit fullscreen mode

In addition to our migration, schema, and model, we will also need a controller to hold our actions as well as a resource route that our front end can send requests to.

rails g controller manual_methods controller
Enter fullscreen mode Exit fullscreen mode
Rails.application.routes.draw do
    resources :my_resources
    # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
  end
Enter fullscreen mode Exit fullscreen mode

Whew, that's done. Now, imagine that we have an application with multiple models. In that case, we would need to repeat this entire tedious process for each of those models. Sounds like a lot of work, right? Well, it doesn't have to be. Thanks to rails' handy-dandy generate resource task, we can focus on the functionality of our application while rails handles the creation of our resources under the hood!

Resource Method

Let's utilize the rails g resource task to achieve the same end result as in our previous example.

rails g resource resource_method attribute_1 attribute_2:integer
Enter fullscreen mode Exit fullscreen mode

Rails will now do the work of creating our migration, model, controller, and resource routes. Pretty cool!

Image description

Once our migration has been created, we can see that it contains our attribute names and data types. All that's left for us to do is send our migration up.

rails db:migrate
Enter fullscreen mode Exit fullscreen mode

Conclusion

Rails g resource is a powerful tool for developers to utilize as it streamlines the process of building our database structure. Thanks to the work that rails does under the hood, we can spend less time structuring our database and more time working on the functionality of our application.

Top comments (0)