rails-engine/notifications

Unable to create notifications for user's followers

Opened this issue · 2 comments

Hi! I implemented follow relationships to my app with this tutorial.

While trying to create notifications for user's followers whenever the person they follow posts a new chapter but i keep getting error
User(#137278760) expected, got #<ActiveRecord::Associations::CollectionProxy [#<User id: 3, name: "Otunba Olusaga of Saganation", username: "saga", email: "example@example.com", created_at: "2019-03-26 11:59:16", updated_at: "2019-03-26 16:40:18", admin: false, bio: "">]> which is an instance of User::ActiveRecord_Associations_CollectionProxy(#137276280)

here is the code:
after_commit :create_notifications, on: :create

private

def create_notifications
    Notification.create do |notification|
        notification.notify_type = 'chapter'
        notification.actor = self.book.user
        notification.user = self.user.followers
        notification.target = self
        notification.second_target = self.book
    end
end

You can't create Notification by assign a collection for notification.user, it's not supports that.

For this case, you need to map the followers (Or use a batch insert tool) to insert multiple notifications.

Here is an example:

https://github.com/ruby-china/homeland/blob/master/app/models/reply/notify.rb#L19