# How to Access Rails Models in a Rake Task

*Published on May 19, 2024*

Tags: Rails Internals

---


If you’ve been working with Rails for a while, you must have come across&nbsp;[Rake](https://github.com/ruby/rake). In fact, the very first post on this blog was&nbsp;[a brief introduction to Rake](/a-brief-introduction-to-rake). Written by the late Jim Weirich (my Ruby hero), Rake is to Ruby what&nbsp;[make](https://www.gnu.org/software/make/manual/make.html)&nbsp;is to C. It makes it very easy to write tasks to simplify your development workflows.

However, one question that many new Rails developers have when learning Rails (I did, too) is&nbsp;**how to access the Rails models or other domain classes in a Rake task**, which is very useful for running some application logic or performing database queries via the command line. This article shows you how.

## Accessing Models in Rake Tasks

Just add the&nbsp;`:environment`&nbsp;task as a dependency for your custom Rake task. It's that simple.

```ruby
task count_users: [:environment] do
  puts User.count
end
```

That's it. Now you can access any models in the task.

## Long Answer: Load Rails Environment

Here’s a simple Rake task that tries to use an ActiveRecord model named&nbsp;`User`.

```ruby
# lib/tasks/count.rake

desc "count the number of users in the system"
task :count_users do
  puts User.count
end
```

If I try to run this Rake task as it is, Rails throws a&nbsp;`NameError`, as it doesn’t know what&nbsp;`User`&nbsp;refers to. It has no idea about the context of our Rails application. No constants (classes and stuff) have been loaded so far.

```bash
$ bin/rails count_users

rails aborted!
NameError: uninitialized constant User

  puts User.count
       ^^^^^^^^^^^^^^^
```

To make the ActiveRecord models available in the Rake tasks, we must tell Rails to load the environment before running the task. This is what happens when you launch the Rails console.

> Loading the Rails environment gives you access to the ActiveRecord models, database, and much more.

Rails provides an&nbsp;`:environment`&nbsp;task to load the environment. In Rake terminology,&nbsp;**your task is**&nbsp;_**dependent**_&nbsp;**on the**&nbsp;**`:environment`**&nbsp;**task**, as it must be performed before your task.

![rake-task.webp](https://writesoftwarewell.com/media/q1ho5hha0h24yt10t09ohlu64781.webp "The environment task in Rails")

```ruby
# lib/tasks/count.rake

desc "count the number of users in the system"
task count_users: [:environment] do
  puts User.count
end
```

Now you can access the&nbsp;`User`&nbsp;model in the rake task without any errors.

But we don't have to stop here. Let's go one step further and explore exactly what the&nbsp;`environment`&nbsp;task is doing.

## How Environment Task Works

The&nbsp;`:environment`&nbsp;task is defined in the&nbsp;`railties/lib/rails/application.rb`&nbsp;file. All it does is load the&nbsp;`config/environment.rb`&nbsp;file in your application, which is what Rails does when you launch your application.

Here's a simplified version of the underlying code.

```ruby
# railties/lib/rails/application.rb

task :environment do
  require_environment!
end

def require_environment!
  environment = paths["config/environment"].existent.first
  require environment if environment
end
```

Note: The&nbsp;[`existent`](https://api.rubyonrails.org/classes/Rails/Paths/Path.html#method-i-existent)&nbsp;method returns all expanded paths but only if they exist in the filesystem.

Upon loading, the&nbsp;`environment.rb`&nbsp;file will first require the&nbsp;`application.rb`&nbsp;file and then call the&nbsp;`initialize`&nbsp;method on your application. This is exactly how your Rails application boots up when you launch it.

```bash
# config/environment.rb

# Load the Rails application.
require_relative "application"

# Initialize the Rails application.
Rails.application.initialize!
```

Once the Rails application loads, it provides access to all the constants in your application, and that's how you can access your Rails models inside a Rake task.

