Rails Generators
Published: 2023-02-03 23:11:46

Table of Contents 
- Introduction 
- Types of Generators 
- Model
- Controller 
- Migration 
- Resource 

**Introduction**
When using Rails a very common task one deals with regularly is creating models which will need to be connected to a database via Active record. Rails provides us with built in generator which helps write some of the code needed in this process. In addition to simplifying the process, rails generators are less time consuming, can help prevent errors and bugs, and allow us to develop via command line interface. 


**Types of Generators**
Rails has a plethora of generators that are accessible and useful when developing. The most important/utilized generators are Model, Controller, Migration, and Resource. 

**Model**
The rails Model generator is extremely prominent as it produces both a model and a migration file. The Migration file is used to set up the database table and the model file is used to handle the logic for the database. One major use of handling the logic for the database would be establishing a connection between two models. For example, if you had two models, one called "team" and the other called "players", in the team model you would write "has_many :players, and in the players model you would write "belongs_to :team.  Then the only other thing you would need to do is add a foreign key to the the players model with the "team_id".  Which creates the association between the player and the team.  

To syntax to create a model generator is as follows:
`rails g model Team name`
- this would create a migration for the teams table and a class for Team. 
- In the teams table, we would have one column with team name, which by default would be a string type.  However, if we wanted to add another column with a different type, such as a integer instead of a string we would do as follows
- `rails g model Team name rank:integer`

- The next step would be to use the model generator again for players:
`rails g model Player name position number:integer team_id:integer`
- Now are team two tables are connected since every player we create will have a team_id telling us which team they are on. Or we could see all the players on a specific team. 

**Controller**
The controller generator is also quite useful, as it coordinates the interaction between the user and the model. It is used to query a model for data from its database, and then it displays the desired results for the user. Rails again simplifies the process for us by implementing a standardized naming pattern to determine how routing should be created. All that needs to be done is add "resources :teams" to the routes file. This then gives us access to these standardized routes. 

To syntax to create a controller generator is as follows:
`rails g controller Teams`
- this would create our TeamsController, where we could request certain information about all teams or a specific team and display that data. 

Inside the Teams Controller we can then make use of the standardized routes, such as index, create, update, and destroy to get commonly needed/requested data:

<u>The index method</u> is a get request where we display a list of all objects for that controller, where its url path is "/teams".

`def index 
        team = Team.all
        render json: team
  end `
- This would display all the instances of team in the teams table. 

<u>The create method</u> is a way of adding a new object (or team, in this case) to the server/database through a post request, where its url path is "/teams". 
`def create 
        team = Team.create(name: params[:name], rank: 
        params[:rank])
        render json: team
  end `
- This would create a new team on the server with whatever information we passed it in the body of the post request, and return us that newly created team

<u>The update method</u> is a way of updating a specific object that is already on the server using its id, where its url path would be "/teams/:id". 
`def update 
        team = Team.find_by(id: params[:id])
        team.update(rank: params[:rank])
        render json: team
  end `
- This would allow us to update the rank of a specific team in the database. 

Finally we have <u>the destroy method</u>, where we can delete a team object from the database, where is url path would be "/teams/:id"
`def destroy  
        team = Team.find(params[:id])
        team.destroy
        head :no_content
  end `
- This would allow us to delete a certain instance of teams using a specific team id. 

**Migration**
We've already seen how migrations can be created using the model generator.  But using the migration generator is a good way of adding, removing, or fixing existing tables. For example, if we wanted to add some more info for our players table we could do so like so:
`rails g migration AddHometownToPlayers`
- Then inside this migration file we would just add:
`def change 
   add_column :players, :hometown, :string
end`
- This is telling us we are adding a column to players, that column being hometown, and its type is a string. 
- All thats left to be done is run 
`rails db:migrate`
and it will update the table, which we can verify in our schema.rb file. 


**Resource** 
Lastly, we have our resource generator which encompasses all of the previous generators we've discussed thus far. It generates a model, controller, and migration for us all with one line of code.  Which just shows how helpful rails generators can be.  
`rails g resource Team name rank:integer`
- We now have a teams table, team model, and a teams controller. Making the resource generator the most efficient of all.