From e885a335b13d229c0713101fddc36d4a24b540c1 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Wed, 16 Oct 2019 17:44:10 -0500 Subject: [PATCH] Add Log SQL Queries Executed By ActiveRecord as a rails til --- README.md | 3 +- ...og-sql-queries-executed-by-activerecord.md | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 rails/log-sql-queries-executed-by-activerecord.md diff --git a/README.md b/README.md index f784d8e..94cdd8b 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [til.hashrocket.com](https://til.hashrocket.com/). -_851 TILs and counting..._ +_852 TILs and counting..._ --- @@ -512,6 +512,7 @@ _851 TILs and counting..._ - [Hash Slicing](rails/hash-slicing.md) - [Ignore Poltergeist JavaScript Errors](rails/ignore-poltergeist-javascript-errors.md) - [List The Enqueued Jobs](rails/list-the-enqueued-jobs.md) +- [Log SQL Queries Executed By ActiveRecord](rails/log-sql-queries-executed-by-activerecord.md) - [Mark A Migration As Irreversible](rails/mark-a-migration-as-irreversible.md) - [Make ActionMailer Synchronous In Test](rails/make-action-mailer-synchronous-in-test.md) - [Mark For Destruction](rails/mark-for-destruction.md) diff --git a/rails/log-sql-queries-executed-by-activerecord.md b/rails/log-sql-queries-executed-by-activerecord.md new file mode 100644 index 0000000..7b9c080 --- /dev/null +++ b/rails/log-sql-queries-executed-by-activerecord.md @@ -0,0 +1,28 @@ +# 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]] +=> # 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)