noticed icon indicating copy to clipboard operation
noticed copied to clipboard

NoMethodError (undefined method `notifications'

Open wissem1210 opened this issue 4 years ago • 6 comments

I m trying to send notification after creating a task and i get this error: NoMethodError (undefined method `notifications' Anyone can help?

wissem1210 avatar Mar 22 '21 09:03 wissem1210

You'll have to provide some details.

excid3 avatar Mar 22 '21 12:03 excid3

Hi @excid3, I'm facing the same issue undefined method 'notifications'. Previously things were working perfectly. I just added multi-tenancy using 'act-as-tenant' gem to my models like spam_monitor.

MubasharNisar avatar Apr 29 '21 12:04 MubasharNisar

You'll have to add some details. Half an error message isn't much to debug with. 😜

excid3 avatar Apr 29 '21 12:04 excid3

First, thank you @excid3 for this great gem! and your screencast. I watched it a while ago and was looking for an excuse to add it to a project. Now, I have found one.

I also ran into this error message and can provide some details and how I solved it. I originally ran the rails generate noticed:model and got an error

The name 'Notification' is either already used in your application or reserved by Ruby on Rails. Please choose an alternative or use --skip-collision-check or --force to skip this check and run this generator again.

I have a view_component named Notification, which handles alert/flash messages. I attempted to run the generator with both --skip-collision-check and --force as suggested, but without luck. So, I renamed it to ActivityNotification. I added the has_many association to my User model, but renamed it to :activity_notifications. This is where my mistake was. I got this error when trying to run OnboardingCompleted.with(contributor: contributor).deliver(current_user).

To solve this, add:

has_many :notifications, as: :recipient, class_name: 'ActivityNotification'

Hope this helps someone.

mattwr18 avatar Jun 09 '22 15:06 mattwr18

Thanks @mattwr18. I haven't actually tested much with other model names, so we'll take a look. 👍

excid3 avatar Jun 09 '22 15:06 excid3

I'm going to update the README with these notes but another option if you want to use a custom name for the notification model you can do the following:

rails g noticed:model ActivityNotification

# Then in your `User` model add:
class User < ApplicationRecord
   has_many :activity_notifications, as: :recipient
end

# NOTE: If you are using the database delivery method you will need to also add:
class CommentNotification < Noticed::Base
    deliver_by :database, association: :activity_notifications
end

cjilbert504 avatar Jun 09 '22 20:06 cjilbert504