I spent some time figuring out how to do this. The easiest approach I found was to use the StaffActionLogger class to add an entry to the UserHistory Table. To use the StaffActionLogger class you need to initialize an object with an admin user from your site. For example, on my site, my user ID is 1, so I initialize the object and assign it to a variable with:
logger = StaffActionLogger.new(User.find(1)) # use your user ID instead of 1 here.
With an initialized StaffActionLogger, you can then use Blake’s code, but add a suspension reason and a line to enter a record into the UserHistory table:
suspend_till = DateTime.new(2057,1,1)
reason = 'Your Suspension Reason'
users = User.where(last_seen_at: nil, id: 1..Float::INFINITY, admin: false)
users.each do |u|
u.suspended_till = suspend_till
u.suspended_at = DateTime.now
u.save!
logger.log_user_suspend(u,reason)
end
The suspension reason will be displayed on the user’s card and profile page. Suspended users will see the reason if they attempt to login to the site. This will also add an entry to your Staff Action logs for each suspended user.
One thing to note, if you are running the above code in the rails console, you’ll need to initialize the logger variable before copying the rest of the code into the console