How to show SQL queries run in the Rails console?
When I run queries (e.g.
record.associated_things) in the console, how can I see the actual database queries being run so I can gain more understanding of what is happening?
Enter this line in the console:
ActiveRecord::Base.logger = Logger.new(STDOUT)
Enter this line in the console:
ActiveRecord::Base.connection.instance_variable_set :@logger, Logger.new(STDOUT)
How to see MySQL queries in rails console, Yes, this can be achieved through redirecting rails log to standard output. Write these in your console prompt: ActiveRecord::Base.logger� Show SQL queries in Rails console. GitHub Gist: instantly share code, notes, and snippets.
In Rails 3+ you can use ActiveRecord::Relation’s
User.where(:id => 3).to_sql #=> "SELECT \"users\".* FROM \"users\" WHERE \"users\".\"id\" = 3"
Display SQL in Rails console, and after runnining a query you'll see SQL queries with the time it took to execute it in your irb session. irb(main):001:0> User.last User Load (0.6� def execute_statement(sql) results = ActiveRecord::Base.connection.execute(sql) if results.present? return results else return nil end end Using execute_statement will return the records found and if there is none, it will return nil. This way I can just call it anywhere on the rails application like for example:
There is the
.explain method in Rails 4.
.to_sql works too, but won't show includes)
Category.includes(:products).explain => EXPLAIN for: SELECT "categories".* FROM "categories" 0|0|0|SCAN TABLE categories EXPLAIN for: SELECT "categories_products".* FROM "categories_products" WHERE "categories_products"."category_id" IN (1, 2) 0|0|0|SCAN TABLE categories_products EXPLAIN for: SELECT "products".* FROM "products" WHERE "products"."id" IN (1, 2, 3, 4, 5, 6, 7) 0|0|0|SEARCH TABLE products USING INTEGER PRIMARY KEY (rowid=?) 0|0|0|EXECUTE LIST SUBQUERY 1
Show SQL queries in Rails console � GitHub, Execute this in the Rails console and then all SQL queries from ActiveRecord will be shown: ActiveRecord::Base.logger = Logger.new(STDOUT). Comment on� You can execute raw query using ActiveRecord. And I will suggest to go with SQL block . query = <<-SQL SELECT * FROM payment_details INNER JOIN projects ON projects.id = payment_details.project_id ORDER BY payment_details.created_at DESC SQL result = ActiveRecord::Base.connection.execute(query)
As from recently, you can use this:
It consists of developer console panel add-on for chrome, and gem file which needs to be added to your application's Gemfile like this:
group :development do gem 'meta_request' end
Then run again:
Restart your application, open it, and launch developer console, and you should see it like this:
Show SQL queries in rails console, Show SQL queries in rails console. When you set log_level :info (mostly on production server), the SQL outputs are disabled in rails console . If you want to see SQL generated by Model. count , What you need is setting log level manually. Run the above query using SQL server management studio. The result will be different than below screenshot. Output Details: TEXT: The query is being executed. session_id: Session id assigned to query. We can use this id to kill this query status: Current status of the query Start_time: The time query was started.
Starting from Rails 6 there is more convenient approach: simply add
ActiveRecord::Base.verbose_query_logs = true in console and you will see all SQL calls and places where it was called. More info https://guides.rubyonrails.org/debugging_rails_applications.html#verbose-query-logs
How to turn on SQL debug logging for ActiveRecord, How to turn on SQL debug logging for ActiveRecord If you want to see the SQL queries that your apps are running and want o optimize them or tune them, then it is Rails Guide about debugging; ActiveRecord logger class� I would trace the Rails code for a normal update to see what it's doing aside from the actual query. Using prepared queries can save you a small amount of time in the database, but unless you're doing this a million times in a row, you'd probably be better off just building the update with normal Ruby substitution, e.g.
Active Record Queries, You can join tables manually or query using associations set up by Rails. How do you check whether a database already contains a record? use something like the Rails Console ( $ rails console ) to test them out, because the queries that return Relation objects, when called, these will run SQL queries immediately. Is there a way to disable SQL query logging when I'm executing commands in the console? Ideally, it would be great if I can just disable it and re-enable it with a command in the console. I'm trying to debug something and using "puts" to print out some relevant data. However, the sql query output is making it hard to read.
Database Views and How to Use Them in a Rails Based Application , After calling our database view (by executing raw SQL query) we get only Now, we can go back to the Rails console and try to use the newly� The SQL Console shows the SELECT statement which was used to retrieve the data for the Data Preview. In the following steps you will adjust the SELECT statement to read the payment status of the order and the company name from the buyer.
Active Record Query Interface — Ruby on Rails Guides, Fire the SQL query and retrieve the corresponding results from the database. This would be useful, for example, if you wanted to run a batch process using a For more information on the dangers of SQL injection, see the Ruby on Rails�