LoginSignup
18
5

More than 5 years have passed since last update.

railsのcallbackに引数を入れる (after_create, before_create, before_build...など)

Posted at

背景

callbackに引数を入れたい

知らないときは、Methodをわざわざ呼んでいた

app/models/user.rb
class User < ApplicationRecord
  after_create :send_notification

  def send_notification
    send_notification_with_args(%i[name account])
  end
end

解決策

app/models/user.rb
class User < ApplicationRecord
  after_create -> { send_notification_with_args(%i[name account]) }
end

一行ですんだ。

18
5
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
18
5