has_many 和belongs_to 与连接表关联?
我有一个“人员”表和一个“通知”表。
有很多人,每个人都可能收到 6 个通知。
我正在考虑有一个通知表,其中包含: 'ID' 'notifitcation_name'
但每个人都可以更改每个通知的某些特征。
然后我有一个带有
“person_id”的 连接表 '通知id' 'notification_text'
这看起来合适吗?
那么Person has_many通知和通知belongs_to Person?
I have a Persons table and a Notifications table.
There are lots of people and 6 notifications that each can potential receive.
I'm thinking of having a Notifications table with just:
'id'
'notifitcation_name'
But each person can change certain characteristics about each notification.
then I'd have a join table with
'person_id'
'notification_id'
'notification_text'
Does that seem appropriate?
Then Person has_many Notifications and Notification belongs_to Person?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你走在正确的轨道上,只是做了一些小改变。
我将创建一个
NotificationType
,其中包含一个人可能收到的 6 种通知。然后您就有了Notification
,它将NotificationType
链接到带有实际消息的Person
。所以你的桌子看起来像
,你的模型看起来像
希望这有帮助。
I think you are on the right track, with a few small changes.
I would create a
NotificationType
, which contains the 6 possible notifications a person could receive. And then you have theNotification
which links theNotificationType
to thePerson
with an actual message.So your tables would look something like
and your models would be
Hope this helps.
您正在寻找的是
has_and_belongs_to_many
协会。编辑:
这实际上可能不是您想要的。当你说“加入表”时我误解了你。您应该有一个人员表和一个通知表,人员有许多通知,并且通知属于人员,正如您所说。但在通知表中,只需存储您所说的放入连接表中的内容:
notification_id
、person_id
、notification_text
以及通知的任何其他属性需要有。What you're looking for is a
has_and_belongs_to_many
association.EDIT:
This actually probably isn't what you want. I misunderstood you when you said "join table". You should have a Persons table and a Notifications table and Person has many Notifications and Notification belongs to Person as you said. But in the Notifications table just store what you said to put in the join table:
notification_id
,person_id
,notification_text
, and any other attributes the notification needs to have.