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
Suppose the following data is included
Column name | data |
---|---|
name | test1 |
[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
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
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
-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
Cherry book https://qiita.com/leavescomic1/items/99f32f45cd04035f146c https://qiita.com/tbpgr/items/56eb65c0ea5882abbb07 https://qiita.com/right1121/items/c74d350bab32113d4f3d