Role Suggesting Name

Consider a CreditCard class:

class CreditCard < ActiveRecord::Base
  belongs_to :brand_manager, class_name: 'User'

class User < ActiveRecord::Base
  has_many :credit_cards, foreign_key: 'brand_manager_id'

It is used like this:


User in this context would be too generic. In the domain-specific language of this application, the people with credit cards are referred to as “brand managers”.

Why not name the model BrandManager?

In this case, User is overloaded to handle three different roles using simple flags on the model. This allows us to use Clearance normally and keeps authentication and standard user vocabulary available where it makes sense.

class Impression < ActiveRecord::Base
  belongs_to :campaign, class_name: 'Offer'

In this example, the object plays two different roles depending on to whom it is being displayed. The object is an offer to recipients but its Role Suggesting Name is campaign to advertisers and impressions.

Learn more about Role Suggesting Name in Philippe Hanrigou’s talk, What the Ruby craftsman can learn from the Smalltalk master.

Pair with one of our expert developers to level up your skills with Coaching by thoughtbot. Save time learning best practices and techniques for reducing technical debt in Ember, Ruby, Haskell, and Go in 1-on-1 sessions tailored to your goals.