A question about relationships

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.

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.

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?

[email protected] 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.

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.

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs