Medium Rails

Use has_many :through over HABTM

Rule Description

Always use `has_many :through` instead of `has_and_belongs_to_many`:
- Allows adding attributes to the join model
- Provides a model for the join table (validations, callbacks)
- More flexible for future requirements
- Easier to query and understand

```ruby
# Good
class User < ApplicationRecord
has_many :memberships
has_many :teams, through: :memberships
end

# Avoid
class User < ApplicationRecord
has_and_belongs_to_many :teams
end
```

Included in Rulesets

Rails Standards
36 rules • 1 standard
View
Code Review Standards
58 rules • 0 standards
View

Add This Rule

Sign in to add this rule to your workspace

Sign in with GitHub

Details

Severity
Medium
Category
Rails
Used in
2 rulesets