rails-antipattern-voyeuristic-model
Installation
SKILL.md
Antipattern: Voyeuristic Model
The smell
- Callers traversing 3+ associations to read data:
order.customer.address.city - Same chain repeated across views, controllers, mailers
&.peppered through chains to dodge nil
Why it hurts
- A nil anywhere raises
NoMethodError on nil - Renaming or restructuring an association breaks every call site
- Views and controllers grow knowledge of model internals
- Hard to mock or stub for tests
The fix
- Use
delegatefor thin pass-throughs (delegate :city, to: :customer) - Add a method that expresses the intent, not the path (
invoice.billed_to) - Tell, don't ask: push the question down to the object that owns the data
- Ensure non-optional
belongs_to(Rails default) so&.isn't needed