使用相同的连接表,通过多个关联进行多次关联回调
所以这可能是非常糟糕的形式。我对 Rails 比较陌生。我不知道。
我有一个项目模型,我希望有许多所有者(他们可以读取和写入所有内容)和许多合作者(他们可以读取和写入一些内容)。
在我的 project.rb 文件中,我有:
has_many :project_user_relationships, :dependent => :destroy
has_many :collaborators, :through => :project_user_relationships, :source => :user
has_many :project_owners_relationships, :class_name => "ProjectUserRelationship", :foreign_key => "project_id",
:before_add => Proc.new { |p,owner_r| owner_r.owner = true }, :conditions => "`project_user_relationships`.owner = true"
has_many :owners, :through => :project_owners_relationships, :source => :user
所以这工作得相当好。如果我添加新所有者,该用户也是协作者,这正是我想要的。我不确定如何解决的问题是,如果我添加一个已经是协作者的用户作为所有者,我会在连接表中得到两个条目。我希望它只是修改已有的记录。我该怎么做?
So this might be really bad form. I'm relatively new to rails. I'm not sure.
I have a project model and I want there to be many owners (who can read and write everything) and many collaborators (who can read and write some stuff).
In my project.rb file I have:
has_many :project_user_relationships, :dependent => :destroy
has_many :collaborators, :through => :project_user_relationships, :source => :user
has_many :project_owners_relationships, :class_name => "ProjectUserRelationship", :foreign_key => "project_id",
:before_add => Proc.new { |p,owner_r| owner_r.owner = true }, :conditions => "`project_user_relationships`.owner = true"
has_many :owners, :through => :project_owners_relationships, :source => :user
So this works reasonably well. If I add a new owner, that user is also a collaborator which is what I want. The issue I'm not sure how to solve is if I add a user that is already collaborator as an owner, I get two entries in the join table. I'd like for it to just amend the record that's already there. How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我建议的数据模型:
然后,成员资格表将具有以下属性:
在成员资格类上定义的范围:
以及用于 User 实例的特殊方法:
将允许您检索用户拥有的项目。 owned_projects 调用。
只需调用 user.projects 即可查看用户协作或拥有的项目。
使用此数据模型可以实现更好的数据规范化,并使用简单的布尔属性来定义用户是否是项目所有者。
此数据模型用于此项目,除了 s/Project/Group/ 之外,还有一些处理邀请用户加入项目的附加功能。
这并没有回答您的“真正的问题”,但我认为问题的一部分是,需要一个将协作者作为所有者存储在同一个表中的数据模型,以最大限度地减少冗余以及管理两个单独表的需要。
Here's the data model I would suggest for this:
And then the membership table will have the following attributes:
A scope defined on the membership class:
And a special method for User instances:
will allow you to retrieve a user's owned projects with the user.owned_projects call.
And just a call to user.projects to see a user's projects that they either collaborate on or own.
You have better data normalization with this data model, and a simple boolean attribute to define whether or not a user is a project owner.
This data model is used in this project, with the exception that s/Project/Group/, and there's some additional functionality to handle inviting users to the Project.
This doesn't answer your "real question", but I think part of the issue is that a data model where collaborators are owners are stored in the same table is needed to minimize redundancies and the need to manage two separate tables.