Home > Enterprise >  rails aborted "Don't know how to build task 'AdminUser.create!()"
rails aborted "Don't know how to build task 'AdminUser.create!()"

Time:11-24

I recently added the gem Active Admin to my rails applicaction (using rails 5.2.6), I got it to work with the default admin and it's working as intended. But I'm unable to add new admins with AdminUser.create!(email: "[email protected]", password: "password", password_confirmation: "password") as I get the rails aborted error and then Don't know how to build task 'AdminUser.create!(email: "[email protected]", password: "password", password_confirmation: "password").

I already tried using rails db:migrate and running my seeds. Also I am using the devise gem.

Full trace:

    rails aborted!
Don't know how to build task 'AdminUser.create!(email: example@gmail.com, password: password, password_confirmation: password)' (See the list of available tasks with `rails --tasks`)
/var/lib/gems/2.7.0/gems/rake-13.0.6/lib/rake/task_manager.rb:59:in `[]'
/var/lib/gems/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:159:in `invoke_task'
/var/lib/gems/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/var/lib/gems/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `each'
/var/lib/gems/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block in top_level'
/var/lib/gems/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:125:in `run_with_threads'
/var/lib/gems/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:110:in `top_level'
/var/lib/gems/2.7.0/gems/railties-5.2.6/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/var/lib/gems/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
/var/lib/gems/2.7.0/gems/railties-5.2.6/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/var/lib/gems/2.7.0/gems/railties-5.2.6/lib/rails/command.rb:48:in `invoke'
/var/lib/gems/2.7.0/gems/railties-5.2.6/lib/rails/commands.rb:18:in `<main>'
/var/lib/gems/2.7.0/gems/bootsnap-1.8.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
/var/lib/gems/2.7.0/gems/bootsnap-1.8.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
/var/lib/gems/2.7.0/gems/bootsnap-1.8.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/var/lib/gems/2.7.0/gems/bootsnap-1.8.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
/var/lib/gems/2.7.0/gems/bootsnap-1.8.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
/home/magus/Desktop/Proyecto ing software/grupo-56/bin/rails:9:in `<top (required)>'
/var/lib/gems/2.7.0/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `load'
/var/lib/gems/2.7.0/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `call'
/var/lib/gems/2.7.0/gems/spring-2.1.1/lib/spring/client/command.rb:7:in `call'
/var/lib/gems/2.7.0/gems/spring-2.1.1/lib/spring/client.rb:30:in `run'
/var/lib/gems/2.7.0/gems/spring-2.1.1/bin/spring:49:in `<top (required)>'
/var/lib/gems/2.7.0/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `load'
/var/lib/gems/2.7.0/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `<top (required)>'
/usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:72:in `require'
/usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:72:in `require'
/home/magus/Desktop/Proyecto ing software/grupo-56/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

As I said before, all the other ActiveAdmin functionalities are working as intended, it's just that I can't create new admins.

What am I doing wrong/could have forgotten to do? Thanks in advance.

CodePudding user response:

There are several ways to add that admin into the database:

  1. rails console - Just open the console and execute AdminUser.create!(...).
  2. seeds.rb - Open the db/seeds.rb file and paste AdminUser.create!(...). Then run rake db:seed. Note that running rake db:seed multiple times will create that admin multiple times - it's best you have some sort of validations or use AdminUser.find_or_create_by(...) instead.
  3. rake task - create a rake file in lib/tasks, name is not important but it should end in .rake (ex: update.rake)
task :add_admin do
  AdminUser.find_or_create_by(email: "[email protected]", password: "password", password_confirmation: "password")
end

Run it with rake add_admin.

If you want that admin only for yourself, your local machine, use the console approach, otherwise pick the other two but make sure the rake tasks are idempotent.

  • Related