-
Notifications
You must be signed in to change notification settings - Fork 1
Associating To Notifications
See The Notification Object for more information on what you can do with a notification object.
NotifyOn automatically attaches an association to the model in which you've called notify_on
(the trigger).
For example, if you added notifications to a Message
model, then an instance of a message—let's call it message
—would have access to its notifications through message.notifications
.
The association eager loads the recipient
and sender
associations on the notification to avoid running into N+1 problems.
When it comes to the recipient (to
), you must manually set the notifications association. However, there's a nice quick way to do so.
Add the following in the model representing objects that receive notifications:
receives_notifications
This will provide an association, notifications
, to that model. For example, say you have an object current_user
that has received notifications. You can get to those notifications via current_user.notifications
.
These notifications are also eager loaded so you can access the sender
and trigger
objects without running into N+1 problems.