ruby 103 lines · 2 tabs

Rails service objects for business logic

Maya Patel Jan 2026
2 tabs
module Posts
  class CreateService
    def initialize(author:, params:)
      @author = author
      @params = params
    end

    def call
      ActiveRecord::Base.transaction do
        create_post
        attach_images if @params[:images].present?
        notify_followers
        schedule_publication if @post.scheduled?

        Result.success(post: @post)
      end
    rescue ActiveRecord::RecordInvalid => e
      Result.failure(errors: e.record.errors.full_messages)
    rescue => e
      Rails.logger.error "Failed to create post: #{e.message}"
      Result.failure(errors: ['An unexpected error occurred'])
    end

    private

    def create_post
      @post = @author.posts.create!(
        title: @params[:title],
        body: @params[:body],
        status: @params[:status] || 'draft'
      )
    end

    def attach_images
      @params[:images].each do |blob_id|
        @post.images.attach(blob_id)
      end
    end

    def notify_followers
      return unless @post.published?

      NotifyFollowersJob.perform_later(@post.id)
    end

    def schedule_publication
      PublishPostJob.set(wait_until: @post.publish_at).perform_later(@post.id)
    end
  end

  class Result
    attr_reader :data, :errors

    def self.success(data = {})
      new(success: true, data: data)
    end

    def self.failure(errors:)
      new(success: false, errors: errors)
    end

    def initialize(success:, data: {}, errors: [])
      @success = success
      @data = data
      @errors = errors
    end

    def success?
      @success
    end

    def failure?
      !@success
    end
  end
end
2 files · ruby Explain with highlit

Service objects encapsulate complex business logic that doesn't belong in models or controllers. Each service performs one operation, like creating a post with side effects, processing a payment, or importing data. I create services in app/services with a single public call method. Services return result objects indicating success/failure with data or errors. This pattern keeps controllers thin—they orchestrate but don't implement business logic. Services are easily testable in isolation and reusable across controllers, jobs, and rake tasks. For multi-step operations, I chain services or use saga patterns. This architecture scales well as applications grow.


Related snips

ruby
class CommentsController < ApplicationController
  before_action :set_post

  def create
    @comment = @post.comments.build(comment_params)

System test: asserting Turbo Stream responses

rails hotwire turbo
by codesnips 4 tabs
ruby
class Post < ApplicationRecord
  belongs_to :author, class_name: 'User'
  has_many :comments, dependent: :destroy

  scope :published, -> { where.not(published_at: nil).where('published_at <= ?', Time.current) }
  scope :draft, -> { where(published_at: nil) }

ActiveRecord scopes for reusable query logic

rails activerecord patterns
by Alex Kumar 1 tab
ruby
module Api
  module V1
    class UsersController < BaseController
      def show
        user = User.includes(:profile).find(params[:id])

ETags for conditional requests and caching

rails caching http-caching
by Alex Kumar 1 tab
ruby
class PostsController < ApplicationController
  def index
    @posts = Post.includes(:author)
                 .order(created_at: :desc)
                 .page(params[:page])
                 .per(10)

Turbo Frames: infinite scroll with lazy-loading frame

rails turbo hotwire
by codesnips 4 tabs
ruby
require "csv"

class PeopleCsvStream
  include Enumerable

  HEADERS = %w[id full_name email signed_up_at plan].freeze

Resilient CSV Export as a Streamed Response

rails performance streaming
by codesnips 3 tabs
erb
<form data-controller="query-sync" data-action="change->query-sync#apply">
  <select name="status" class="rounded border p-2">
    <option value="">Any</option>
    <option value="open">Open</option>
    <option value="closed">Closed</option>
  </select>

Filter UI that syncs query params via Stimulus (no front-end router)

rails hotwire stimulus
by Henry Kim 2 tabs

Share this code

Here's the card — post it anywhere.

Rails service objects for business logic — share card
Link copied