---
title: Role Suggesting Name
teaser:
tags: web,rails,good code
author: Dan Croak
published_on: 2009-09-18
---

Consider a `CreditCard` class:

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

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

It is used like this:

```ruby
brand_manager.credit_cards
credit_card.brand_manager
```

`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](http://rdoc.info/projects/thoughtbot/clearance) normally and keeps
authentication and standard `user` vocabulary available where it makes sense.

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

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](http://www.amazon.com/Smalltalk-Best-Practice-Patterns-ebook/dp/B00BBDLIME/ref=tmm_kin_title_0)
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](http://mwrc2009.confreaks.com/14-mar-2009-15-35-what-the-ruby-craftsman-can-learn-from-the-smalltalk-master-philippe-hanrigou.html).
