结合acts_as_follower与activity_notification无法接收通知的问题排查
acts_as_follower and activity_notification Hey there! Let's break down what's going wrong with your notification setup and get it working properly.
First, let's look at the core issue in your original acts_as_notifiable configuration. Your Follow model doesn't have a direct user attribute—instead, it uses polymorphic associations for follower (the user doing the following) and followable (the user being followed). That's why your initial targets lambda isn't working: follow.user doesn't exist!
Your proposed rewrite also won't work because current_user.follow isn't a valid association, and the lambda parameters for acts_as_notifiable are (notifiable, key) (not current_user, key). The notifiable here is the Follow instance being created.
Step 1: Correct the Follow Model Configuration
Let's fix the acts_as_notifiable setup to target the right user (the one being followed). Here's the corrected code:
class Follow < ActiveRecord::Base extend ActsAsFollower::FollowerLib extend ActsAsFollower::FollowScopes belongs_to :followable, polymorphic: true belongs_to :follower, polymorphic: true def block! self.update_attribute(:blocked, true) end acts_as_notifiable :users, # Target the user being followed (follow.followable is the User instance) targets: ->(follow, _key) { [follow.followable].compact }, tracked: true, # Optional: Set the path users land on when clicking the notification notifiable_path: ->(follow, target_user) { user_path(follow.follower) } end
targets: ->(follow, _key) { [follow.followable].compact }: This ensures the notification is sent to the user being followed (since that's who would want to know someone followed them). The.compacthandles any edge cases wherefollowablemight be nil.notifiable_path: This adds a custom link to the notification (taking the target user to the follower's profile). You can adjust this to whatever makes sense for your app.
Step 2: Ensure Your User Model is Configured as a Notification Target
You need to tell activity_notification that your User model is a valid recipient for notifications. Add this to your User model:
class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable # Required for acts_as_follower functionality acts_as_follower acts_as_followable # Required for activity_notification to send notifications to users acts_as_notification_target end
Step 3: Verify Database Setup
Make sure you've run the activity_notification installation and migrations. If you haven't, run these commands:
rails generate activity_notification:install rails db:migrate
This creates the necessary tables (like notifications) to store notification data.
Step 4: Test the Flow
When a user follows another user (e.g., current_user.follow(other_user)), a Follow record is created. With tracked: true in your Follow model, activity_notification should automatically generate a notification for other_user.
You can check if notifications are being created by running this in your Rails console:
Notification.last
If it exists and has the correct target (the user being followed), your setup is working.
Common Pitfalls to Check
- Polymorphic Association Types: Ensure that when you call
current_user.follow(other_user), thefollowable_typein thefollowstable is set to"User"(no typos!). - Devise Current User: Double-check that your controllers have access to
current_user(Devise should handle this out of the box, but custom controllers might need extra setup).
内容的提问来源于stack exchange,提问作者Fernando Aureliano

