Using interactors in Rails

Interactor gem

Let’s do a quick review.

What is an Interactor?

An interactor is a simple, single-purpose object.

Interactors are used to encapsulate your application’s business logic.

Each interactor represents one thing that your application does.


An interactor is given a context. The context contains everything the interactor needs to do its work.

When an interactor does its single purpose, it affects its given context.

Let’s get started!

gem "interactor", "~> 3.0"


Here, we’re setting up something like steps of interactors to follow.

Here we’re using OrderInit and AddLog interactors, that means our flow will follow this chain.

class AddOrder
  include Interactor::Organizer

  organize OrderInit, AddLog

On the controller level, let’s call our AddOrder organizer and pass order_params as a parameter.

def create
  @order =
  result =
  return render status: :bad_request, json: result.error unless result.success?
  render status: :ok, json: result.order

Well, easy part: OrderInit should contain call method which uses context as parameter.

Let’s add simple validations here (we can call fail on context).

class OrderInit
  include Interactor

  def call!(error: 'Please add title') if context[:title].empty?

    order = Order.create(title: context[:title], total_price: context[:total_price])! unless order.valid?
    context.order = order

Last step is AddLog interactor, here we just showing log message for the order, using context.

class AddLog
  include Interactor

  def call"ADDING ORDER WITH TOTAL PRICE: #{context[:total_price]}")

That’s it! Easy and simple.