sujay
Wed Nov 22 2023
Add the `:environment` task as dependency for Rake task to access models.
desc "count number of users"
task :count_users do
puts User. count # NameError: uninitialized constant User (NameError)
end
To make the models available, tell Rails to load the environment before running the task.
desc "count number of users"
task count_users: [:environment] do
puts User. count
end