用户和嵌套的(但可能独立的)“组”在 Rails 3 中?
我在思考某些事情时遇到了一些困难。基本上,我正在开始一个新项目,其中涉及一组组组组组……嗯,你明白了。
无论如何,整个应用程序中唯一有点“通用”的模型是用户的概念(因为它们决定了任何人拥有的权限)。当您的组可以“拥有”其他组时,问题就出现了。例如,您可以拥有一个由“国家”分会拥有的“州”分会拥有的“城市”分会,等等。并且,每个分会都可以拥有自己的用户,然后有权访问其下面的所有组。
但问题是,没有一个组可以保证被另一组拥有(或使用),因此它们需要独立管理。我无法完全概念化处理这种性质的事情的方法是什么。我的意思是我可能会使用某种acts_as_nested 设置,但我担心即使这样也可能会失控。另外,就组本身而言,我是否应该使用某种继承模型(假设它们可能共享许多属性)?
也许我应该简单地为每个组创建一个单独的 MVC 设置——尽管仍然存在关联用户等问题。任何人都可以提供建议吗?
最好的
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个自引用的组表。这会将所有组数据保存在一个表中,并且魔法是通过关联完成的。
Ryan Bates 有一个railscast,可能会根据您的情况进行调整。例如,拥有所有权或类似的东西而不是友谊。
编辑:
只有单亲父母会有所帮助。看来插件 acts_as_tree 可能适合手头的任务。 Ryan Bates 恰好也有一个用于该插件的 railscasts epidsode。
目标
1:允许群体有孩子、父母解决方案:acts_as_tree
2:允许群体有可能成为或保持独立。 解决方案:acts_as_tree。只需删除parent_id,该组将成为它自己的根节点
3:强制层次结构(例如,城市不能将另一个城市作为父级)。 解决方案:这可能必须通过 自定义验证
4:用户权限继承。我预计这个特定目标是最具挑战性的。当然,我不知道“权利”对你的申请意味着什么,但我会做一些假设......你在我错误的地方填上空白。一旦您有了用户和组之间的关联(以及可能的“权限”表)。 解决方案:要确定权限是否应适用于 current_group 的 current_user,a) 检查所有权,如果不是,b) 向上遍历 current_group.ancestors,直到得到答案或找到根。
这听起来是一个有趣的项目。祝你好运!
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!