[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] Introduction of initial data
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] 1 model CRUD (Routing Main)
[Ruby on Rails] model, controller terminal command
Ruby on Rails model creation / deletion command
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] 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
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
Implementation of Ruby on Rails login function (Session)
Deploy to Ruby on Rails Elastic beanstalk (EB deploy)
[Ruby on Rails] Until the introduction of RSpec
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] Individual display of error messages
[Ruby on Rails] How to make the link destination part of the specified id
[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] Asynchronous communication of posting function, ajax
[Introduction] Try to create a Ruby on Rails application
Method summary to update multiple columns [Ruby on Rails]
[Ruby on Rails] How to write enum in Japanese
Implementation of Ruby on Rails login function (devise edition)
[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
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 ~
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
Ruby on rails learning record -2020.10.03
Deploy to Ruby on Rails Elastic beanstalk (IAM permission change)
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
<Dot installation> Introduction to Ruby on Rails5 Source code comparison
(Ruby on Rails6) Display of the database that got the id of the database
Ruby on rails learning record-2020.10.07 ②
Delete all the contents of the list page [Ruby on Rails]
A note about the seed function of Ruby on Rails
[Ruby on Rails] Easy scroll animation of javascript (using ScrollReveal.js)
How to display a graph in Ruby on Rails (LazyHighChart)