0

I'm trying to send a notification to a user with Mailboxer, and I need to pass an object along which will affect how the notification is displayed in my navbar's notification dropdown.

@recipient.notify("#{current_user.name} needs you to review his help with: #{@offer.title}", "#{@message}", @offer)

The last argument is where I'm trying to pass the object, @offer.

This is the Mailboxer method which I'm trying to use:

 def notify(subject,body,obj = nil,sanitize_text=true,notification_code=nil,send_mail=true)
    Mailboxer::Notification.notify_all([self],subject,body,obj,sanitize_text,notification_code,send_mail)
  end

It calls this notify_all method:

def notify_all(recipients, subject, body, obj = nil, sanitize_text = true, notification_code=nil, send_mail=true)
  notification = Mailboxer::NotificationBuilder.new({
    :recipients        => recipients,
    :subject           => subject,
    :body              => body,
    :notified_object   => obj,
    :notification_code => notification_code
  }).build

  notification.deliver sanitize_text, send_mail
end

When I try to access object with this: <%= notification.object_id %>, I get a long number like 205981093. And I get an error if I try to access one of the offer object's fields with this: <%= notification.object_id.title %>

`undefined method `title' for 2166878920:Fixnum`

I'm not even sure if this is the way Mailboxer Notifications are used. I've had a very difficult time finding info on them. Any help is greatly appreciated.

domi91c
  • 1,962
  • 4
  • 23
  • 38

2 Answers2

3

As indicated by https://github.com/mailboxer/mailboxer/blob/master/app/models/mailboxer/notification.rb

Notification.rb provides a polymorphic association for notified object.

belongs_to :notified_object, :polymorphic => :true

So you need specify notifications as an Association in Object model which you want to pass. For ex here you can have in Offer.rb

has_many :notifications , as: :notified_object.
aysyal31
  • 31
  • 4
2

Try the following to get your unread notifications

current_user.mailbox.notifications(:read => false)
Johan Baaij
  • 584
  • 1
  • 5
  • 15
  • 1
    I know how to get unread notifications. I was trying to find out how to attach an object to a notification. I just ended up using Public Activity for my notification system, which is working well now. – domi91c Sep 22 '14 at 19:21
  • Ah I see, my bad. I Believe what you are talking about is being discussed over here; http://stackoverflow.com/questions/22914420/adding-belongs-to-relationship-to-ruby-gem-mailboxer – Johan Baaij Sep 23 '14 at 07:10