has_many 和belongs_to 与连接表关联?

发布于 2024-10-17 23:57:57 字数 269 浏览 2 评论 0原文

我有一个“人员”表和一个“通知”表。

有很多人,每个人都可能收到 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 技术交流群。

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

发布评论

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

评论(2

抽个烟儿 2024-10-24 23:57:57

我认为你走在正确的轨道上,只是做了一些小改变。

我将创建一个 NotificationType,其中包含一个人可能收到的 6 种通知。然后您就有了 Notification,它将 NotificationType 链接到带有实际消息的 Person

所以你的桌子看起来像

Person: id, name, ...
NotificationType: id, name, severity, ...
Notification: id, person_id, notification_type_id, message

,你的模型看起来像

class NotificationType
  has_many :notifications
end

class Person
  has_many :notifications
end

class Notification
  belongs_to :person
  belongs_to :notification_type
end

希望这有帮助。

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 the Notification which links the NotificationType to the Person with an actual message.

So your tables would look something like

Person: id, name, ...
NotificationType: id, name, severity, ...
Notification: id, person_id, notification_type_id, message

and your models would be

class NotificationType
  has_many :notifications
end

class Person
  has_many :notifications
end

class Notification
  belongs_to :person
  belongs_to :notification_type
end

Hope this helps.

感性不性感 2024-10-24 23:57:57

您正在寻找的是 has_and_belongs_to_many 协会。

编辑
这实际上可能不是您想要的。当你说“加入表”时我误解了你。您应该有一个人员表和一个通知表,人员有许多通知,并且通知属于人员,正如您所说。但在通知表中,只需存储您所说的放入连接表中的内容:notification_idperson_idnotification_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.

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