[Ruby on Rails] Elimination of Fat Controller-First, logic to model-

Introduction

This is the first post. I learned Ruby on Rails at school and had a refactoring curriculum, but at that point I was desperate to complete it, and I hadn't refactored it, so this time I moved the controller logic to Model and made the controller cleaner. I will write the method.

What is Fat Controller in the first place?

It refers to the state of the controller where the number of lines of the controller itself is large and it is difficult to follow the processing flow (simply speaking, the visibility is bad). At school, I wrote all the logic in the Controller without being aware of it at all, but I am still fighting with the personal application Fat Controller because I want to get closer to the code at the practical level (laughs).

Carving procedure

First of all, let's feel like moving all the logic of the controller that has expanded enormously to Model. Actions are sufficient for public methods described in Controller.

And I think that there is basically no problem with Model as the destination to move the logic.

Example: If you want to pull only the items that the user has

Before carving ↓

ruby:users.controller.rb


def show
  @useritems = Item.includes(:images).where(user_id:(current_user.id)).order(id: "DESC") if user_signed_in?
end

Since the authentication function is implemented using devise, the id of the currently logged-in user can be obtained with "current_user.id". In other words, I want to pull the user's item information to the user's My Page (show action) that is currently logged in. However, this data acquisition, in fact, is also used in the index action of "items.controller.rb". So I would like to define a method in Model and put together a description to get the user's item.

First of all, Controller

ruby:users.controller.rb


def show
  @useritems = Item.user_items_get(current_user.id) if user_signed_in?
end

Since "current_user" cannot be used on the Model side, pass it as an argument.

Then Model

Item.rb


def self.user_items_get(user_bigint)
  Item.includes(:images).where(user_id:(user_bigint)).order(id: "DESC")
end

By doing this, even the index action of ItemController

ruby:items.cotroller.rb


def index
  @useritems = Item.user_items_get(current_user.id).limit(10) if user_signed_in?
end

It can be used like this.

When it comes to writing a method in Model and calling it in Controller, beginners tend to feel that it is a big deal, but it is important to make Controller clean, so please use it. (The number of lines in this example hasn't decreased ... lol)

Recommended Posts

[Ruby on Rails] Elimination of Fat Controller-First, logic to model-
Basic knowledge of Ruby on Rails
How to use Ruby on Rails
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
[Ruby on Rails] Introduction of initial data
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] 1 model CRUD (Routing Main)
[Ruby on Rails] model, controller terminal command
Let's summarize "MVC" of Ruby on Rails
Ruby on Rails model creation / deletion command
part of the syntax of ruby ​​on rails
Deploy to Heroku [Ruby on Rails] Beginner
Preparing to introduce jQuery to Ruby on Rails
[Ruby on Rails] Japanese notation of errors
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Ruby on Rails] Model test with RSpec
Explanation of Ruby on rails for beginners ①
[Ruby on rails] Implementation of like function
[Ruby on Rails] Button to return to top
Environment construction of Ruby on Rails from 0 [Cloud9] (From Ruby version change to Rails installation)
How to solve the local environment construction of Ruby on Rails (MAC)!
How to debug the processing in the Ruby on Rails model only on the console
[Ruby On Rails] How to search the contents of params using include?
Implementation of Ruby on Rails login function (Session)
[Ruby on Rails] How to display error messages
[Ruby on Rails] Until the introduction of RSpec
How to add / remove Ruby on Rails columns
Recommendation of Service class in Ruby on Rails
Ruby on Rails ~ Basics of MVC and Router ~
[Ruby on Rails] A memorandum of layout templates
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
[Ruby on Rails] Individual display of error messages
[Ruby on Rails] How to make the link destination part of the specified id
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
Ruby on Rails <2021> Implementation of simple login function (form_with)
[Introduction] Try to create a Ruby on Rails application
Method summary to update multiple columns [Ruby on Rails]
Docker the development environment of Ruby on Rails project
[Ruby on Rails] How to change the column name
[Updated from time to time] Ruby on Rails Convenient methods
[Ruby on Rails] Change URL id to column name
[Ruby on Rails] Implementation of tagging function/tag filtering function
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
[Ruby On Rails] How to reset DB in Heroku
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
[Ruby on Rails] From MySQL construction to database change
Explanation of Ruby on rails for beginners ② ~ Creating links ~
(Ruby on Rails6) How to create models and tables
[Ruby on Rails] Search function (model, method selection formula)
Try using the query attribute of Ruby on Rails
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
[Ruby On Rails] Correct description location of unique constraint that gives uniqueness to DB
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
[Ruby On Rails] How to search and save the data of the parent table from the child table