Rails 3,使用 has_many 和多态性,还是其他什么?

发布于 2024-10-11 23:36:29 字数 598 浏览 10 评论 0原文

我有一个无法解决的问题。问题是我有一个用户模型和一个事件模型,并且在事件模型中用户可以拥有多个角色。我们还可以将服务员添加为现有用户,或者创建一个可以转换(仅通过数据库中的标志)为真实用户的假用户。

So the diffrent user-type are:
A existing  user who has created the event (this is a one-to-one relationship)
A existing  user who is admin for the event
A existing user who has paid money on the event
A existing user who attended the event
and lastly a person who attended the event but isn't a user yet but has to be shown, so a fake-user would do here..

当我添加出席用户/假用户时,我使用嵌套表单。

那么有人知道如何解决这个问题吗? 我尝试过 habtm、has_many :through 以及多态性和 STI,但没有成功。 提前致谢!

I have a problem that I can't solve. The problem is that I've got one user-model, and one event-model and in the event model a user can have multiple role's. We can also add attendants either an existing user, or create a fake user that can be converted (just by a flag in the db) to a real user.

So the diffrent user-type are:
A existing  user who has created the event (this is a one-to-one relationship)
A existing  user who is admin for the event
A existing user who has paid money on the event
A existing user who attended the event
and lastly a person who attended the event but isn't a user yet but has to be shown, so a fake-user would do here..

When I add the attending users/fake users I use nested forms.

So does anyone have a idea for how to solve this?
I have tried with habtm, has_many :through with polymorphism and STI but with no luck.
Thanks in advance!

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

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

发布评论

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

评论(1

白龙吟 2024-10-18 23:36:29

你想要一种多种关系,但我认为你不需要它。你不能使这个多态。这是我建议的设置:

对于您的事件表列:

  • id
  • Creator_id (FK 到 users.id)
  • (特定于事件的字段)

按原样保留现有用户表...

现在,有趣的部分 - users_events 表:

  • id
  • user_id ( FK users.id)
  • event_id (FK events.id)
  • is_admin? (布尔值)
  • is_paid? (布尔值)
  • 是否有人参加? (布尔值)

至于处理不存在的出席用户问题 - 如果您不关心用户的数据,只需在适用的情况下将 user_id 保留为 NULL 即可。如果您想要数据,我建议使用您已经建议的内容 - 布尔标志。

You want a multi-habtm relationship, but I don't think you need it. You can't make this polymorphic. Here is my suggested setup:

For your events table columns:

  • id
  • creator_id (FK to users.id)
  • (event-specific fields)

Keep your existing users tables as is...

Now, the fun part - the users_events table:

  • id
  • user_id (FK users.id)
  • event_id (FK events.id)
  • is_admin? (boolean)
  • is_paid? (boolean)
  • is_attended? (boolean)

As for handling your non-existent attending user problem - if you aren't concerned with the user's data, just leave user_id NULL where applicable. If you want the data, I suggest using what you already suggested - a boolean flag.

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