0

I'm having a bit of trouble wrapping my head around something. Basically I'm starting a new project that involves groups of groups of groups of groups of…well, you get the idea.

Anyway, the only model that is somewhat "universal" throughout the application is the concept of Users (as they are what determines what privileges any one person has). The problem comes in when you have groups that can "own" other groups. For instance, you can have a "city" chapter that is owned by a "state" chapter that is owned by a "national" chapter, etc. And, each chapter can have their own users when then have rights to all groups below them.

The thing is, though, that no one group is guaranteed to be owned (or used) by another group, so they would need to be independently managed. I can't quite conceptualize what the methodology would be to handle something of this nature. I mean I could probably use some sort of acts_as_nested setup, but I fear even that may get out of hand. Also, as far as the groups themselves are concerned, should I use some kind of inheritance model (given that they would likely share many properties)?

Perhaps I should simply create an individual MVC setup for each group -- although there's still the issue of associating users, etc. Can anyone offer suggestions?

Best

humble_coder
  • 2,777
  • 7
  • 34
  • 46

1 Answers1

0

You could create a groups table which self-references. That would keep all your group data in a single table and the magic is done via associations.

Ryan Bates has a railscast that could likely be tweaked to fit your situation. e.g. rather than friendships, having ownerships or something like it.

Edit: Having only a single parent helps. It appears the plugin acts_as_tree will likely work for the task at hand. Ryan Bates happens to have a railscasts epidsode for that plugin as well.

Objectives

1: Allow groups to have children, parents Solution: acts_as_tree

2: Allow groups to potentially become or remain independent. Solution: acts_as_tree. Simply remove the parent_id and the group would become it's own root node

3: Enforce hierarchy (e.g cities cannot have another city as a parent). Solution: This would likely have to be done via a custom validation

4: Users rights inheritance. I anticipated this particular objective to be the most challenging. Of course, I don't know all the in's and outs of what "rights" means to your application, but I'll some assumptions... you fill in blanks where I'm wrong. Once you have the associations between users and groups (and possible "rights" table). Solution: To determine if rights should apply to current_user for current_group, a) check ownership and if not b) transverse up current_group.ancestors until you get an answer or hit the root.

It sounds like a fun project. I wish you luck with it!

DonaldSowell
  • 396
  • 1
  • 10
  • Yeah, I'd considered that. My only concern now is if a particular group ever diverges (hence the "potentially independent" in the title) from the original "GROUP" structure. I'm somewhat of a normalization freak and often worry preemptively (and needlessly) about that kind of stuff. I suppose we could just make groups sufficiently generic and cross that bridge when we get there. One other thing is the fact that group ownership *must* be linear (e.g. Country owns State owns City) so I'd need to build that in as well somehow. Type column with business rules in Group model? – humble_coder May 26 '11 at 15:58
  • How many group types could you have? Also, how many owners or parents can a particular group have? – DonaldSowell May 26 '11 at 18:21
  • Currently there are 3 Group types: City Chapter, State Chapter, and National Chapter. But in the future we could insert new groups above (e.g. International), below (e.g. Neighborhood), or between (e.g. County). I just need a quality model that will support the ability for ownership and users (logins) to be associated with them. All ownership will be linear. So National can have many State and State can have many City. Basically 1-to-many the whole way down. – humble_coder May 26 '11 at 19:56