Skip to content

Instantly share code, notes, and snippets.

@webuilder240
Last active April 18, 2023 13:13
Show Gist options
  • Save webuilder240/0ceec0b1b3f429e3a2ec8aa9c08d4788 to your computer and use it in GitHub Desktop.
Save webuilder240/0ceec0b1b3f429e3a2ec8aa9c08d4788 to your computer and use it in GitHub Desktop.
1は条件分岐をキューイングする際、Jobクラス内部のどちらででも行うようにしています。2はJobクラスでのみ処理条件を書くようにしています。
# user.rb
class User < ApplicationRecord
after_create_commit do
if is_admin?
AdminUserCreateLoggingJob.perform_later(self.id)
end
end
end
# ConditionをJobクラスに含めない
class AdminUserCreateLoggingJob < ApplicationJob
queue_as :default
def perform(user_id)
user = User.find_by!(id: user_id, is_admin: true)
Rails.logger.info "Admin user #{user.name} logging..."
end
end
# user.rb
# キューイング時に処理条件は書かない。
class User < ApplicationRecord
after_create_commit do
AdminUserCreateLoggingJob.perform_later(self.id)
end
end
# ConditionをJobクラスに含める
class AdminUserCreateLoggingJob < ApplicationJob
queue_as :default
def perform(user_id)
user = User.find(user_id)
if user.is_admin?
Rails.logger.info "Admin user #{user.name} logging..."
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment