rails-engine / notifications

🛎 Notifications Center engine like GitHub or other application for any Rails applications.
MIT License
389 stars 42 forks source link

Unable to create notifications for user's followers #24

Closed ghost closed 5 years ago

ghost commented 5 years ago

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
huacnlee commented 5 years ago

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

ghost commented 5 years ago

Thank you so much for this incredible tip. Can you please 🙏 explain a little bit more with some more practical code. I really don't get the one in the notify.RB file, thank you man!

On Wed, Mar 27, 2019, 12:14 PM Jason Lee notifications@github.com wrote:

Closed #24 https://github.com/rails-engine/notifications/issues/24.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rails-engine/notifications/issues/24#event-2232724802, or mute the thread https://github.com/notifications/unsubscribe-auth/AZBJLk9B6U2qJCnoDDHCzGI5Bwcb9HGYks5va1J_gaJpZM4cMlWl .