Users, Roles, and naming the join model
I recently was working on some code for a security model. It was pretty standard, with Users and Roles and lots of other stuff that felt like overkill.
I hit a bit of a stubbing block when it came to naming my join model. A user has many roles through what??
In the end I copied my database and went with privileges. The result was
class User
has_many :privileges
has_many :roles, :through => :privileges
end
class Privilege
belongs_to :role
belongs_to :user
end
class Role
has_many :privileges
has_many :users, :through => :privileges
end
I find that coming up with good names that accurately reflect the intent as one of the biggest programming challenges.
It also got me thinking about how to model responsibilities, even though they they weren’t needed is this particular case. I think that it would be something like:
class User
has_many :great_powers, :conditions => {:great => true}
has_many :great_responsibilities, :through => :great_powers
end