Rails HABTM 和belongs_to 用于关联用户/项目

发布于 2024-11-17 19:26:24 字数 1648 浏览 5 评论 0原文

我有一个 UserProject 模型。一个用户可以有多个项目,一个项目可以有多个用户。当然,使用 has_and_belongs_to_many 关联就很简单。我的问题是我还想跟踪创建该项目的用户。对于我的 Project 模型来说,使用 belongs_to :user 会很简单。那么问题是,当用户已经与 Project 具有 HABTM 关系时,为 User 执行 has_many :projects 没有什么意义。

这就是我最终想要实现的目标:

# building a project with the currently logged in user
current_user.projects.build(...)

# now when a user wants to add another user to this project
project = current_user.projects.find(...)
project.users << User.find(...)

# grabbing information
some_project.user  #=> The user who created this project
some_project.users #=> The array of User objects associated
some_user.projects #=> The array of Project objects associated

将来,User 可能会与 Group 具有相同的关系,其中 User > 都会有很多并且属于很多组。但我还想追踪这个群组创建者

我很可能错过了一些东西。实现这一目标的常见方法是什么?

如果有助于澄清,下面列出了可能的配置:

    • has_and_belongs_to_many:用户
    • 属于:用户
    • has_many:项目
    • 只有用户才能创建
  • 用户
    • has_and_belongs_to_many :组
    • has_many:项目
    • 可以支持一个团队或一个项目,也可以同时属于两者
  • 项目
    • has_and_belongs_to_many:用户
    • 属于:组
    • 用户都应该能够创建项目

我也尝试过为每个关联做简单的 has_and_belongs_to_many 并包括一个 creator_id 例如,并自己跟踪。例如.. Project.create(:creator_id => current_user.id) 但这看起来很黑客,我确信它们会是一个更好的方法来做到这一点。如果我错过了一些简单的事情,请为我的无知道歉。

I have a User and Project model. A user can have multiple projects, and a project can have multiple users. This of course is simple with a has_and_belongs_to_many association. My problem is that I'd also like to track the user who created the project. This would be simple with a belongs_to :user for my Project model. The problem then is that doing a has_many :projects for a User when a user already has a HABTM relationship with a Project makes little sense.

Here's what I would eventually like to achieve:

# building a project with the currently logged in user
current_user.projects.build(...)

# now when a user wants to add another user to this project
project = current_user.projects.find(...)
project.users << User.find(...)

# grabbing information
some_project.user  #=> The user who created this project
some_project.users #=> The array of User objects associated
some_user.projects #=> The array of Project objects associated

In the future it's likely a User will have the same sort of relationship with a Group, where a User would both have many and belong to many groups. But again I would also like to track the creator of this Group.

It's quite likely I'm missing something. What's a common way to achieve this?

If it helps to clarify, a possible configuration is laid out below:

  • Group
    • has_and_belongs_to_many :users
    • belongs_to :user
    • has_many :projects
    • Only a User should have the ability to create a Group
  • User
    • has_and_belongs_to_many :groups
    • has_many :projects
    • Can great a group or a project, and can also belong to both
  • Project
    • has_and_belongs_to_many :users
    • belongs_to :group
    • both a User and a Group should have the ability to create a Project

I've also tried doing simple has_and_belongs_to_many for each association and including a creator_id for example, and tracking this myself. For example.. Project.create(:creator_id => current_user.id) but this seemed hackish and I was sure they would be a better way to do this. Apologies for my ignorance if I've missing something simple.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

贱贱哒 2024-11-24 19:26:24

再次阅读本文,您希望该项目有一个所有者,可以是组或用户。将其存储为项目模型中的多态belongs_to关系。 IE。所有者类型=> '用户'/'组'owner_id => 123

与该组关联的其他用户是多对多的,因此您需要一个连接表。

您的组模型是一个单独的模型,与用户具有多对多的关系,因此您可以简单地将创建它的用户存储为组模型的字段,即。 :用户 ID 123。

Reading this again you want to have one owner of the project, either a group or a user. Store that as a polymorphic belongs_to relationship from the project model. ie. owner_type => 'User'/'Group' owner_id => 123

Other users associated with the group are many to many so you need a join table.

Your group model is a separate model with a many to many relationship with users so you can simply store the user who created it as a field of the group model ie. :user_id 123.

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