> For the complete documentation index, see [llms.txt](https://ploegert.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ploegert.gitbook.io/til/programmy/rails/log-sql-queries-executed-by-activerecord.md).

# Log SQL Queries Executed By ActiveRecord

When entering ActiveRecord statements in a Rails console, it can be useful to see what SQL queries are being executed under the hood.

```ruby
> Book.first
  Book Load (25.6ms)  SELECT  "books".* FROM "books" ORDER BY "books"."id" ASC LIMIT $1  [["LIMIT", 1]]
=> #<Book:0x00007f824ec6ff98
```

Notice the line right after the statement shows the corresponding `select` query.

If you aren't seeing this line of SQL, then you'll need to turn on logging for ActiveRecord. For instance, your test environment may not be configured to log the SQL.

Right in your console, try running the following line:

```ruby
> ActiveRecord::Base.logger = Logger.new(STDOUT)
```

Enter an ActiveRecord statement, you should now be seeing the corresponding SQL queries.

[source](https://stackoverflow.com/a/2936016/535590)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/rails/log-sql-queries-executed-by-activerecord.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
