def migrated_accounts(account) followed = Follow.where(account: account).pluck(:target_account_id) migrated = Account.where(id: followed).where.not(moved_to_account_id: nil) migrated = migrated.where.not(moved_to_account_id: followed) migrated end def autosubscribe_migrated_accounts(account_id) account = Account.find_by(id: account_id) migrated = migrated_accounts(account) Account.where(id: migrated.pluck(:moved_to_account_id)).each do |target| FollowService.new.call(account, target) end end autosubscribe_migrated_accounts(1)