Rails 3,使用 has_many 和多态性,还是其他什么?
我有一个无法解决的问题。问题是我有一个用户模型和一个事件模型,并且在事件模型中用户可以拥有多个角色。我们还可以将服务员添加为现有用户,或者创建一个可以转换(仅通过数据库中的标志)为真实用户的假用户。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你想要一种多种关系,但我认为你不需要它。你不能使这个多态。这是我建议的设置:
对于您的事件表列:
按原样保留现有用户表...
现在,有趣的部分 - users_events 表:
至于处理不存在的出席用户问题 - 如果您不关心用户的数据,只需在适用的情况下将 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:
Keep your existing users tables as is...
Now, the fun part - the users_events table:
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.