Context
Context
Key Features
How to Run Services in the Same Context
Context Rollback
Example:
class User::Create < ApplicationService
# Arguments
arg :attributes, type: Hash
# Steps
step :create_user
step :create_profile
step :send_welcome_email
# Outputs
output :user, type: User
output :profile, type: Profile
def create_user
self.user = User.create!(attributes)
end
def create_profile
service = Profile::Create
.with(self) # This runs the service in the same context
.run(user:)
self.profile = service.profile
end
# If the Profile::Create service fails, this step and any following steps won't execute
# And all database changes will be rolled back
def send_welcome_email
# We don't run this service in the same context
# Because we don't care too much if it fails
service = Mailer::SendWelcomeEmail.run(user:)
# Handle the failure manually if needed
if service.failed?
# Handle the failure
end
end
endContext Arguments
What's Next?
Last updated