A question about relationships

Can you clarify:

As far as I can tell, you're saying that a user (which does not exist yet) may have a single rank. Implicit in a set of ranks is a role.

Without knowing the context and business problem that you are trying to solve, this seems backwards to my entire experience.

Generally I think of rank and role as being either independent, or loosely dependent.

For example, "sergeant" is a rank, whereas "quartermaster" is one of many roles that can be fulfilled by sergeants or other ranks. Another example is a rank of "1st degree black belt", with a role of "instructor", where instructor is one of many roles that may be filled by a 1st degree black belt or other black belt ranks.

Your model seems odd to me also in that one user is only permitted to fill one role and rank. I can't fill the roles of both quartermaster and instructor, or hold both the ranks of sergeant and 1st degree black belt.

Try this:

user HABTM role via table roles_users (what roles the user fills) user HABTM rank via table ranks_users (what ranks the user holds) rank HABTM role via table ranks_roles (allowed rank/role relationships)

At worst, it will give you more flexibility than you need but sufficient information to allow edit checks against the ranks_roles relationship to enforce your particular paradigm. At best, it may resolve problems that you have not yet anticipated.

Unless users pick a role, the set of roles will remain empty. For practical purposes, this is the same as the relationship being nil.

Under your proposed model, you do not need to do anything because, until you assign a role, the role ID is nil.

Will Gant wrote: