使用相同的连接表,通过多个关联进行多次关联回调

发布于 2025-01-02 03:47:04 字数 779 浏览 0 评论 0原文

所以这可能是非常糟糕的形式。我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烂柯人 2025-01-09 03:47:04

这是我建议的数据模型:

class Project < ActiveRecord::Base
    has_many :memberships, :dependent => :destroy
    ...
end

class Membership < ActiveRecord::Base
    belongs_to :project
    belongs_to :user
    ...
end

class User < ActiveRecord::Base
    has_many :memberships, :dependent => :destroy
    has_many :projects, :through => :memberships
    ...
end

然后,成员资格表将具有以下属性:

:id
:user_id
:project_id
:is_owner (boolean)

在成员资格类上定义的范围:

scope :owner, where("is_owner")

以及用于 User 实例的特殊方法:

def owned_projects
    memberships.owner.includes(:projects).inject([]) {|array, m| array << m.project; array}
end

将允许您检索用户拥有的项目。 owned_projects 调用。

只需调用 user.projects 即可查看用户协作或拥有的项目。

使用此数据模型可以实现更好的数据规范化,并使用简单的布尔属性来定义用户是否是项目所有者。

此数据模型用于此项目,除了 s/Project/Group/ 之外,还有一些处理邀请用户加入项目的附加功能。

这并没有回答您的“真正的问题”,但我认为问题的一部分是,需要一个将协作者作为所有者存储在同一个表中的数据模型,以最大限度地减少冗余以及管理两个单独表的需要。

Here's the data model I would suggest for this:

class Project < ActiveRecord::Base
    has_many :memberships, :dependent => :destroy
    ...
end

class Membership < ActiveRecord::Base
    belongs_to :project
    belongs_to :user
    ...
end

class User < ActiveRecord::Base
    has_many :memberships, :dependent => :destroy
    has_many :projects, :through => :memberships
    ...
end

And then the membership table will have the following attributes:

:id
:user_id
:project_id
:is_owner (boolean)

A scope defined on the membership class:

scope :owner, where("is_owner")

And a special method for User instances:

def owned_projects
    memberships.owner.includes(:projects).inject([]) {|array, m| array << m.project; array}
end

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文