1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

インスタンスメソッドと、selfについて調べる

Posted at

ふとたまにみるselfはなんだろう、言語化できないなと
思ったので調べてみることにしました。

あと、インスタンスメソッドにも理解が浅いなと感じたため
調べて記事にしました

以下のモデルを例に進めていきます

app/models/user.rb
class User < ApplicationRecord
  #Article(記事)モデルとの紐付け
  has_many :article
  
  validates :name, presence: true
  validates :email, presence: true 
end

インスタンスメソッド

下記のようなデータが中に入っていたとする

カラム名 データ
name test1
email test1@mail.com
age 19
password test1
app/controllers/users_controller.rb
class UsersController < ApplicationController
  def index
    #IDが1のuserを検索
    @user = User.find(1)
    #「.call_age」とは…
    @my_age = @user.call_age 
  end
end

call_age」メソッドはモデルに書いたメソッド
コントローラーから呼び出しています

app/models/user.rb
class User < ApplicationRecord
  #Article(記事)モデルとの紐付け
  has_many :article
  
  validates :name, presence: true
  validates :email, presence: true 

  def call_age
    if self.age >= 20  #20歳以上の場合
       return "私は大人です" 
    else  #20歳以下の場合
       return "私は子供です"
    end
  end
end

では、インスタンスメソッドとは

その名の通りにインスタンスに対して使用するメソッドのこと

通常のメソッドは、プログラム中からならいつでも呼び出せるのに対して
インスタンスメソッドは、作成されたオブジェクトからしか呼び出すことが出来ない

メソッドの中身を見ていく

そして、メソッドの内容を見ていく

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 "私は大人です" 
    else
       return "私は子供です"
    end
  end
end

コントローラー内の「@user」と、モデル内の「self」は
中身が同じ

id: 1, name: "test1", email: "test1@mail.com", age: 19

という中身が入っています

インスタンスメソッドの結果は、現在「self.age」には「19」というデータが
入っていますので、条件ではelseを通り「私は子供です」という結果が得られます

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 "私は大人です" 
    else
       return "私は子供です"
    end
  end
end

コントローラーにデバックを挟む

app/controllers/users_controller.rb
class UsersController < ApplicationController
  def index
    @user = User.find(1)
    @my_age = @user.call_age 
    #デバックを挟む
    binding.pry
  end
end
[1] pry(#<UsersController>)> @user.call_age 
[2] pry(#<UsersController>)> "私は子供です"

という結果が得られているので大丈夫そうですね

まとめ

・selfは、クラスから作成されているインスタンスと中身が同じ
・インスタンスメソッドは、作成されたオブジェクトからしか呼び出すことが出来ない

それと、コントローラーでも
モデルに書いたインスタンスメソッドを書くことはできます。
しかし、なるべくDBとのやりとりをするようなメソッドはモデルに
書くほうがいい
ので、今回はモデルにメソッドを作成しました。

使い分けれるように勉強していきたいです

参考記事

チェリー本
https://qiita.com/leavescomic1/items/99f32f45cd04035f146c
https://qiita.com/tbpgr/items/56eb65c0ea5882abbb07
https://qiita.com/right1121/items/c74d350bab32113d4f3d

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?