---
title: Separate Processing and Data Access
impact: HIGH
impactDescription: enables testable business logic and better code organization
tags: separation-of-concerns, repository, service, architecture, quality
---

## Separate Processing and Data Access

Keep business logic separate from direct database access. Use Service Objects to handle logic and ActiveRecord models primarily for data access and persistence.

**Incorrect (mixed concerns):**

```ruby
class OrderController < ApplicationController
  def create
    # Business logic in controller
    if params[:amount] > 100
      discount = params[:amount] * 0.1
      total = params[:amount] - discount
    end
    Order.create(amount: total, user_id: current_user.id)
  end
end
```

**Correct (separated concerns):**

```ruby
# app/services/create_order_service.rb
class CreateOrderService
  def initialize(user, params)
    @user = user
    @params = params
  end

  def call
    total = calculate_total(@params[:amount])
    Order.create!(amount: total, user: @user)
  end

  private

  def calculate_total(amount)
    return amount if amount <= 100
    amount * 0.9
  end
end
```

**Tools:** Manual Review
---
