LoginSignup
0
0

More than 1 year has passed since last update.

sendメソッド

Last updated at Posted at 2023-04-11

はじめに

sendメソッドについてどのように使用するかメモを残します

sendメソッドとは

レシーバが持つメソッドを、文字列またはシンボルで指定して呼び出せる機能のこと

実際の使い方

service.rb
class Service < ActiveRecord::Base
   〜省略〜
  def self.twitter
    find_by(name: "twitter")
  end

  def self.instagram
    find_by(name: "instagram")
  end
end
sponsor.rb
class Sponsor < ApplicationRecord
   〜省略〜
  def first_used_at(sns)
    Service.send(sns).created_at
  end
end
sponsors/show.html.slim
class Sponsor < ApplicationRecord
   〜省略〜
  table.table
    tr
      th
      | twitter初期登録日時
      td
        = @spnsor.first_used_at("twitter")
end

解説

@spnsor.first_used_at("twitter")"twitter"部分が、メソッド名と同じなのでこのような書き方ができる

0
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
0
0