Service Objects

It is best practice to separate your business logic into Service Objects rather than shoving all of it into your controllers and models. This keeps each layer focused: controllers handle HTTP, models handle persistence, and services orchestrate the actual work.

1. ActiveInteraction

One solution we really like is ActiveInteraction. It is very stable, has wonderful documentation, and gives you a clean way to build service objects with support for things like composed interactions and even ActiveModel validations.

Add the gem to your Gemfile and run bundle install:

# Gemfile
gem "active_interaction", "~> 5.3"

2. A Base Interaction

Create a base ApplicationInteraction class that all of your service objects can inherit from. This gives you a single place to add shared behavior later:

# app/interactions/application_interaction.rb
class ApplicationInteraction < ActiveInteraction::Base
  # Your interactions will inherit from this class!
end

3. Writing an Interaction

Each interaction declares its inputs with typed filters, runs validations, and implements an execute method. Here is a small example:

# app/interactions/users/sign_up.rb
module Users
  class SignUp < ApplicationInteraction
    string :email
    string :name

    validates :email, presence: true

    def execute
      User.create!(email: email, name: name)
    end
  end
end

Every interaction can be called two ways. .run always returns an outcome — check valid?, then read result or errors. .run! returns the result directly and raises ActiveInteraction::InvalidInteractionError when validation fails:

outcome = Users::SignUp.run(email: "ada@example.com", name: "Ada")

if outcome.valid?
  user = outcome.result
else
  # outcome.errors holds the validation messages
end

# Or, where an exception is the failure mode you want:
user = Users::SignUp.run!(email: "ada@example.com", name: "Ada")

4. Calling It From a Controller

Which one you reach for is decided by who sees the failure. In a controller, a validation failure is user feedback — the email was taken, the date is in the past — so it belongs in a flash message next to a redirect or re-render, never in an exception. Use .run, check the outcome, and surface the errors:

# app/controllers/users_controller.rb
def create
  outcome = Users::SignUp.run(sign_up_params)
  return redirect_to new_user_path, alert: outcome.errors.full_messages.to_sentence unless outcome.valid?

  redirect_to outcome.result, notice: "Welcome aboard!"
end
Never run! in a Controller

.run! turns a validation failure into an unhandled exception — a raw 500 page for the user and an error report for you — for something that was never a bug. The outcome pattern above costs one guard clause and gives the user a message they can act on.

.run! is the right call outside the request cycle, wherever an exception is the correct failure mode and something upstream is prepared to handle it:

  • Background jobs — let the exception fail the job so the queue's retry and dead-letter semantics take over.
  • Seeds and rake tasks — a bad record should halt the run loudly, not leave a half-seeded database behind a swallowed outcome.
  • Tests — run! gives you the result in one line and turns an unexpected validation failure into a failing example with a real message.
Pro Tip

Because interactions support composed interactions, you can call one service from another with compose, building larger workflows out of small, well-tested pieces.

Made with by Profoundry .
Copyright © 2023-2026 Profoundry LLC.
All rights reserved.