开发者

has_many and belongs_to association with join table?

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 t开发者_JAVA技巧able with

'person_id' 'notification_id' 'notification_text'

Does that seem appropriate?

Then Person has_many Notifications and Notification belongs_to 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 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜