[Rails] I don't know how to use the model ...

Background

Currently, I am attending a certain programming school, but when implementing the search function (search window), I was told to use a model and a controller for processing properly. Eh ... It's okay to use all the controllers ... (Of course, I'm actively using them separately now!)

** ・ Controller… Data is acquired from DB, saved, and only simple processing is described ** ** ・ Model: Describes the value obtained from the DB that performs complicated processing ** I use it properly with the image.

However, I didn't want to use it because I couldn't understand why the ** "processing catalog" ** set in the model could be used with the controller, no, I couldn't. (This article is not an article to explain the search function. It is just a summary of my mind about how to use the model.)

Implementation of search function

models/tweet.rb (model)


class Tweet < ApplicationRecord
  validates :text, presence: true
  belongs_to :user
  has_many :comments

  def self.search(search)
    return Tweet.all unless search
    Tweet.where('text LIKE(?)', "%#{search}%")
  end

end

controllers/tweets_controller.rb (controller)


class TweetsController < ApplicationController

  def search
    @tweets = Tweet.search(params[:keyword])
  end

end


Understanding point

① What is the "search" method of the controller?
(Not listed in the reference ??)

It's not listed in the reference. Because ** "method made by model" **! !!

② What is the model "self"?
(When you google, it's commonly called a "class method" ...)

――I won't explain in detail here, but ** class name is assigned ** to this one. (The expression assignment is used for clarity) --Look at the first line of tweet.rb (tweet model). Did you understand the class name? ――In other words, "self.search (search)" is ** "" Tweet.search (search) "**. (Like I saw it somewhere)

③ Why can the controller use the "processing catalog" set in the model?
(Finally the main subject)

Did you understand somehow? ??

--The "Tweet.search (params [: keyword])" used in the controller is the same as the ** self.search (search) "** method you created in the model.

The identity of the method

In fact, various methods such as "find method" and "include method" that we often use Just like I made my own "search method" this time, ** Rails' ActiveRecord made it **! !!

** Each model inherits ActiveRecord **, so you can use methods in your controller! (Strictly speaking, class Tweet <ApplicationRecord <ActiveRecord :: Base) If you look at the first line of tweet.rb (tweet model), you can see it.

That guy

Well, it's getting longer, but there is one who hasn't solved it yet. Yes, what was "self = class method" after all?

Class methods are literally methods that can be used for "classes". Can you remember what a "class" was? ?? If you don't know, take a look at the first line of tweet.rb (tweet model).

** I still don't understand any more! !! Lol**

Acknowledgments

It may have been a bit confusing, but please forgive me because I have been programming for 2 months. (I was able to organize my head relatively.) I've written this for a long time, but thank you for reading to the end.

If the interpretation is wrong, I would be very grateful if you could let me know.

More and more.

Recommended Posts

[Rails] I don't know how to use the model ...
[Rails] How to use the map method
[Rails] How to use enum
How to use rails join
[Rails] How to use validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to use Scope
How to use the link_to method
[Rails] How to use gem "devise"
How to use the include? method
[Rails] How to use devise (Note)
How to use the form_with method
[Rails] How to use flash messages
How to use the wrapper class
How to use Ruby on Rails
[Rails] How to use Active Storage
[Introduction to Rails] How to use render
[Java] How to use the File class
How to use custom helpers in rails
[Ruby on Rails] How to use CarrierWave
[Java] How to use the hasNext function
[Rails] How to use rails console with docker
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
Don't know how to build task'credentials: edit'
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to use MySQL in Rails tutorial
I tried to understand how the rails method "redirect_to" is defined
About the matter that I was addicted to how to use hashmap
[Processing × Java] How to use the class
I tried to understand how the rails method "link_to" is defined
[Rails] Don't use the select method just to narrow down the columns!
[Ruby on Rails] How to use redirect_to
[Note] How to use Rails 6 Devise + cancancan
[Processing × Java] How to use the function
[Ruby on Rails] How to use kaminari
[Java] How to use the Calendar class
[Rails] How to use video_tag to display videos
[Rails] How to use helper method, confimartion
How to use credentials.yml.enc introduced in Rails 5.2
How to debug the processing in the Ruby on Rails model only on the console
[Rails] How to decide the destination by "rails routes"
How to use the camera module OV7725 (ESP32-WROVER-B)
[Rails] When I use form_with, the screen freezes! ??
[Java] How to use Thread.sleep to pause the program
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
[Rails] How to use select boxes in Ransack
How to use rails g scaffold, functions, precautions
Output of how to use the slice method
The code I used to connect Rails 3 to PostgreSQL 10
After all, how should I use Rails callback?
How to use the replace () method (Java Silver)
How to use JQuery in js.erb of Rails6
[Rails] How to use Gem'rails-i18n' for Japanese support
[Ruby on Rails] How to use session method
[Rails] How to use PostgreSQL in Vagrant environment
How to check Rails commands in the terminal
[Ruby basics] How to use the slice method