Find out about instance methods and self

I wonder what self is sometimes seen, I can't verbalize it I thought, so I decided to look it up.

Also, I felt that I didn't understand the instance method. I looked it up and made an article

We will proceed using the following model as an example

app/models/user.rb


class User < ApplicationRecord
  #Article(article)Linking with the model
  has_many :article
  
  validates :name, presence: true
  validates :email, presence: true 
end

Instance method

Suppose the following data is included

Column name data
name test1
email [email protected]
age 19
password test1

app/controllers/users_controller.rb


class UsersController < ApplicationController
  def index
    #Search for user with ID 1
    @user = User.find(1)
    #「.call_What is "age"?
    @my_age = @user.call_age 
  end
end

The "** call_age **" method is the ** method written in the model ** Calling from the controller

app/models/user.rb


class User < ApplicationRecord
  #Article(article)Linking with the model
  has_many :article
  
  validates :name, presence: true
  validates :email, presence: true 

  def call_age
    if self.age >= 20  #20 years and over
       return "I am an adult" 
    else  #Under 20 years old
       return "I am a child"
    end
  end
end

So what is an instance method?

As the name implies, the method used for instance

Whereas regular methods can be called at any time within the program Instance methods can only be called from the created object

Let's look at the contents of the method

And we will look at the contents of the method

app/controllers/users_controller.rb


class UsersController < ApplicationController
  def index
    @user = User.find(1)
    @my_age = @user.call_age 
  end
end

app/models/user.rb


class User < ApplicationRecord
  has_many :article
  
  validates :name, presence: true
  validates :email, presence: true 

  def call_age
    if self.age >= 20
       return "I am an adult" 
    else
       return "I am a child"
    end
  end
end

" @ User "in the controller and" self" in the model The contents are the same

id: 1, name: "test1", email: "[email protected]", age: 19

Contains the contents

As a result of the instance method, the data "** 19 **" is currently in " self.age ". Since it is included, you can get the result "I am a child" through else under the conditions.

app/models/user.rb


class User < ApplicationRecord
  has_many :article
  
  validates :name, presence: true
  validates :email, presence: true 

  def call_age
    if self.age >= 20
       return "I am an adult" 
    else
       return "I am a child"
    end
  end
end

Put debugging in the controller

app/controllers/users_controller.rb


class UsersController < ApplicationController
  def index
    @user = User.find(1)
    @my_age = @user.call_age 
    #Debugging
    binding.pry
  end
end
[1] pry(#<UsersController>)> @user.call_age 
[2] pry(#<UsersController>)> "I am a child"

It seems that it is okay because the result is obtained

Summary

-Self has the same contents as the instance created from the class -Instance methods can only be called from the created object

And even with a controller You can write the instance method you wrote in the model. However, as much as possible ** methods that interact with DB are modeled It's better to write **, so this time I created a method in the model.

I want to study so that I can use it properly

Reference article

Cherry book https://qiita.com/leavescomic1/items/99f32f45cd04035f146c https://qiita.com/tbpgr/items/56eb65c0ea5882abbb07 https://qiita.com/right1121/items/c74d350bab32113d4f3d

Recommended Posts

Find out about instance methods and self
Find out about class methods
Find out about Docker
About Ruby instance methods
About calling instance methods
About pluck and ids methods
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
About instance variables and attr_ *
Find out about annotationProcessor for Gradle 5.0
About Java static and non-static methods
Find out about environment variables (memo)
About the equals () and hashcode () methods
Functions and methods
Ruby: Differences between class methods and instance methods, class variables and instance variables
About Enclosing Instance 2
Find out about Rails hidden_field (create a confirmation screen and check the behavior)
About Ruby methods
About the relationship between HTTP methods, actions and CRUD
About Bean and DI
About classes and instances
About gets and gets.chomp
About redirect and forward
About encapsulation and inheritance
Ruby variables and methods
[Ruby] About instance generation
[Ruby] methods, instance methods, etc ...
About ActiveRecord_Relation and Couldn't find Item without an ID error
Rails6 Couldn't find User with'id' = sign_out and cannot log out
Think about the differences between functions and methods (in Java)