# Bulk creating users with rails etc

**URL:** <https://meta.discourse.org/t/bulk-creating-users-with-rails-etc/281716>\
**Category:** Development\
**Created:** [October 10, 2023, 6:37am UTC](https://meta.discourse.org/t/bulk-creating-users-with-rails-etc/281716 "2023-10-10T06:37:09Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![PrettyGirl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/prettygirl/32/330415_2.png) [@PrettyGirl](https://meta.discourse.org/u/PrettyGirl)\
**Post date:** [October 10, 2023, 6:37am UTC](https://meta.discourse.org/t/bulk-creating-users-with-rails-etc/281716/1 "2023-10-10T06:37:09Z")

</div>

I want to add bulk usernames (to create initial dummy users to create posts).  
So I was trying with creating a rake file, and loading it and then running it. But had no success. The rake file doesn’t even gets loaded sometimes, and when I use another method it gets loaded and then error thrown “I don’t know how to create …” error thrown by `rails c` (can’t even remember the error message as I tried thousand different methods over hours to make it work with no success)

What I did

**Created `users.rake` file**

```plaintext
cd /var/www/discourse  
mkdir -p lib/tasks  
touch lib/tasks/users.rake

```

**Editing it with nano**

```plaintext
cd /var/www/discourse/lib/tasks
sudo nano users.rake

```

**And then pasted the following code into editor and saved**

```plaintext
namespace :users do
  desc "Create users from a list"
  task :create, [:usernames, :password, :domain] => :environment do |task, args|
    usernames = args[:usernames].split(',')
    password = args[:password]
    domain = args[:domain]

    User.transaction do
      usernames.each do |username|
        next if User.exists?(username: username)

        email = "#{username}@#{domain}"
        user = User.new(username: username, email: email, password: password)

        unless user.save
          puts "Failed to save user #{username}: #{user.errors.full_messages.join(", ")}"
          raise ActiveRecord::Rollback
        end
      end
    end
  end
end

```

Loaded into rails c and loaded it into tasks

```plaintext
rails console
load 'lib/tasks/users.rake'
exit

```

**Then tried to create the users**

```plaintext
./launcher enter app
rake users:create["username1,username2", "password", "domain.com"]

```

Is there any other method that I can try through the terminal without using third party things.

---

_[View the full topic](https://meta.discourse.org/t/bulk-creating-users-with-rails-etc/281716)._
