Rails / ActiveRecord 中的多态模型
我对 ActiveRecord 有点生疏,所以如果答案很明显,请原谅我。
我的 Rails 项目中有三个模型对象:Student、Assignment 和 Course。正如您可能想象的那样,课程有has_many
作业。现在我希望能够为学生分配任意数量的课程和个人作业。
我尝试使用基于“可分配”概念的多态关联,但无法使其正常工作。
在 Rails 中建模这些关系的最自然的方法是什么?
I'm a little rusty on my ActiveRecord, so forgive me if the answer is somehow obvious.
I have three model objects in a rails project: Student, Assignment, and Course. As you might imagine, a Course has_many
assignments. Now I want to be able to assign to a Student an arbitrary number of Courses and individual Assignments.
I've tried to use a polymorphic association based on the notion of 'assignable', but can't get it to work properly.
What's the most natural way to model these relationships in rails?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是简单地说学生
has_many
作业和has_many
课程。课程还有has_many
作业。当然,我不知道您想在应用程序中实现什么目标,但课程和作业似乎是完全不同的实体,所以我不确定您为什么想要多态关联。One way to do it is to simply say that a Student
has_many
assignments andhas_many
courses. A Course alsohas_many
assignments. Of course I don't know what you're trying to achieve in your application, but it seems that courses and assignments are completely different entities, so I'm not sure why you'd like a polymorphic association.