Medium
Rails
Use has_many :through over HABTM
Official
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
```
Add This Rule
Sign in to add this rule to your workspace
Sign in with GitHubDetails
- Severity
- Medium
- Category
- Rails
- Used in
- 2 rulesets