---
title: Always define dependent option for associations
impact: HIGH
impactDescription: Prevent orphaned records and ensure data integrity.
tags: rails, database, data-integrity, active-record
---

## Always define dependent option for associations

Prevent orphaned records and ensure data integrity. Always specify the `dependent` option for `has_many` and `has_one` associations to define what happens to associated records when the parent is deleted.

**Incorrect:**

```ruby
class User < ApplicationRecord
  has_many :posts # No dependent option
end
```

**Correct:**

```ruby
class User < ApplicationRecord
  has_many :posts, dependent: :destroy
end
```

**Tools:** Custom linter, Manual Review
