Skip to content

Associating To Notifications

Sean C Davis edited this page Sep 5, 2016 · 2 revisions

See The Notification Object for more information on what you can do with a notification object.

Trigger

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.

Recipient

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.

Clone this wiki locally