# A question about relationships

**URL:** <https://www.ruby-forum.com/t/a-question-about-relationships/69171>\
**Category:** Rails\
**Created:** [September 19, 2006, 4:27am UTC](https://www.ruby-forum.com/t/a-question-about-relationships/69171 "2006-09-19T04:27:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hotgazpacho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a587f6/32.png) [@hotgazpacho](https://www.ruby-forum.com/u/hotgazpacho)\
**Post date:** [September 19, 2006, 4:27am UTC](https://www.ruby-forum.com/t/a-question-about-relationships/69171/1 "2006-09-19T04:27:36Z")

</div>

Hello all,  
I’ve created a pair of entities:  
Role  
Rank

Someone can be added to a role all on their own. Being set to a  
particular rank will automatically add them to the role (at least  
eventually - I don’t have a user class yet, so it’s gonna be a while).  
Anyway, rails was smart enough to pick up on the fact that Rank.role\_id  
is a reference to the roles table. It did not, however, add anything on  
the default view for it.

What I’d like to do is the following:

1. Enable the user seeing the view to select a role by name, if they  
want to do so. Otherwise, I would like for the value to remain nil. This  
is the part that I imagine is difficult.

2. I know I need to set up the relationships in the entities, but I’m  
still not clear on how to do it correctly to make this work. I would  
assume that Role has\_many ranks, and rank belongs\_to one role. Would  
that be correct?

Thanks,  
Will

* * *

Do you Yahoo!?  
Everyone is raving about the all-new Yahoo! Mail.

---

<div class="post-metadata">

**Author:** ![hotgazpacho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a587f6/32.png) [@hotgazpacho](https://www.ruby-forum.com/u/hotgazpacho)\
**Post date:** [September 19, 2006, 8:30am UTC](https://www.ruby-forum.com/t/a-question-about-relationships/69171/2 "2006-09-19T08:30:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hotgazpacho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a587f6/32.png) [@hotgazpacho](https://www.ruby-forum.com/u/hotgazpacho)\
**Post date:** [September 19, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/a-question-about-relationships/69171/3 "2006-09-19T17:05:45Z")

</div>

Right. The user has a single rank. I’m actually going to have to rethink  
this though, as I may want to add users to multiple roles at certain  
ranks. So now I’ll have to break this out into another table.

This is for a martial arts site. When someone gets to a particular rank,  
they’ll be added to a role for that rank. Certain ranks (black belt and  
fifth degree black belt) will also get other roles(namely access to an  
additional forum, and forum moderating privilages). Essentially, their  
rank will be specified as the highest rank that they have earned. They  
will also have all the roles associated with the ranks below that one.

Does that make sense?

removed\_email\_address@domain.invalid wrote:  
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 G. wrote:

> 1. I know I need to set up the relationships in the entities, but I’m still not clear on how to do it correctly to make this work. I would assume that Role has\_many ranks, and rank belongs\_to one role. Would that be correct?  
> Content-Transfer-Encoding: 8bit  
> X-Google-AttachSize: 1116
> 
> Hello all,  
> I’ve created a pair of entities:  
> Role  
> Rank

Someone can be added to a role all on their own. Being set to a  
particular rank will automatically add them to the role (at least  
eventually - I don’t have a user class yet, so it’s gonna be a while).  
Anyway, rails was smart enough to pick up on the fact that Rank.role\_id  
is a reference to the roles table. It did not, however, add anything on  
the default view for it.

What I’d like to do is the following:

1. Enable the user seeing the view to select a role by name, if they  
want to do so. Otherwise, I would like for the value to remain nil. This  
is the part that I imagine is difficult.

2. I know I need to set up the relationships in the entities, but I’m  
still not clear on how to do it correctly to make this work. I would  
assume that Role has\_many ranks, and rank belongs\_to one role. Would  
that be correct?

Thanks,  
Will

> 

* * *

Do you Yahoo!?  
Everyone is raving about the all-new Yahoo! Mail.

> –0-986346046-1158632560=:39774–

* * *

Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+  
countries) for 2Â¢/min or less.

---

<div class="post-metadata">

**Author:** ![hotgazpacho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a587f6/32.png) [@hotgazpacho](https://www.ruby-forum.com/u/hotgazpacho)\
**Post date:** [September 20, 2006, 3:13am UTC](https://www.ruby-forum.com/t/a-question-about-relationships/69171/4 "2006-09-20T03:13:35Z")

</div>

Am I understanding this product fully?

The product is a set of forums and the supporting mechanisms necessary  
to make them work.

Every forum has a predictable set of types of roles. For example:

reader  
poster  
moderator  
administrator

In addition, there are roles that fall outside of the forum role  
structure. For example:

public  
sysadmin  
security

The roles are distinct for each forum. the role “moderator shindo” is  
distinct from “moderator wado kai karate”. When a new forum is  
created, the roles for that forum can also be created as a part of that  
process.

The assignment to a forum based role is generally governed by rank.  
There is a process for assigning rank. The role assignment rules can  
be applied as this process is fired, and as users are registered to the  
individual forums.

Role assignment rules should be based on data in the database.

Given this, you have the following models:

user  
forum  
roletype  
role  
rank

The following relationships apply:

user HABTM roles (table roles\_users) (and vice versa)  
user HABTM ranks (table rank\_users) or user belongs\_to ranks + rank  
has\_many users (I’m partial to user HABTM ranks)  
forum HABTM roles (table forums\_roles) (and vice versa)  
rank HABTM roles (table ranks\_roles) (and vice versa)

Within a forum, each action joins (roles\_users for the current user)  
against (forums\_roles for the current forum and action). If there is  
not an empty set, then the user has authority to perform the action.  
This role test should be implemented as a boolean method, so it can be  
reused both for adapting the options on the display and for wrapping  
the actions to eliminate a security hole.

In the forum create method, the class checks to see if the forum roles  
exist. If they do not then it creates them.

Sysadmin needs authority on all actions, synce sysadmin is the ultimate  
authority. This is best implemented in the forum user role test.

This isn’t complete, but it should be reasonably close.

---

<div class="post-metadata">

**Author:** ![hotgazpacho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a587f6/32.png) [@hotgazpacho](https://www.ruby-forum.com/u/hotgazpacho)\
**Post date:** [September 20, 2006, 12:25am UTC](https://www.ruby-forum.com/t/a-question-about-relationships/69171/5 "2006-09-20T00:25:20Z")

</div>

I understand Martial Arts, and I understand roles.

In martial arts, it is reasonable to hold multiple ranks. For example,  
within  
the Shintani association, one can hold the rank of nidan in karate and  
shodan  
in shindo at the same time.

I still think roles should be distinct from ranks - sysadmin, for  
example, is  
a matter of ownership rather than rank.

Let me ponder for a bit … I see the shape of a solution but can’t  
quite  
express it yet.
