Easy way to suppress SQL output in Rails console?
Asked Answered
A

1

11

I frequently need to go on the Rails console with rails c. Then I run some statement which loops through records of a model. I need to output information, but all the SQL code gets littered throughout as well. Like:

Students.all.each {|s| puts s.inspect unless s.attendance};nil

I put that nil at the end so I don't get an ugly dump of all students. This is the output:

  Student Load (4.3ms)  SELECT "students".* FROM "students"
  Attendance Load (3.6ms)  SELECT "attendances".* FROM "attendances" WHERE "attendances"."id" = 2694 LIMIT 1
  Attendance Load (2.7ms)  SELECT "attendances".* FROM "attendances" WHERE "attendances"."id" = 2695 LIMIT 1
  Attendance Load (4.9ms)  SELECT "attendances".* FROM "attendances" WHERE "attendances"."id" = 2689 LIMIT 1
#<Student id: 3, attendance_id: 2689, teacher_id: 6, began_at: "2013-05-21 19:16:37", finished_at: "2013-05-21 20:34:33", created_at: "2013-05-21 19:16:37", updated_at: "2013-05-21 20:34:33">
  Attendance Load (2.0ms)  SELECT "attendances".* FROM "attendances" WHERE "attendances"."id" = 2692 LIMIT 1
#<Student id: 26, attendance_id: 2713, teacher_id: 6, began_at: "2013-05-21 22:44:25", finished_at: "2013-05-21 22:44:42", created_at: "2013-05-21 22:44:25", updated_at: "2013-05-21 22:44:42">
  Attendance Load (1.6ms)  SELECT "attendances".* FROM "attendances" WHERE "attendances"."id" = 2714 LIMIT 1
#<Student id: 27, attendance_id: 2714, teacher_id: 3, began_at: "2013-05-21 22:45:06", finished_at: "2013-05-21 22:45:27", created_at: "2013-05-21 22:45:06", updated_at: "2013-05-21 22:45:27">
  Attendance Load (4.0ms)  SELECT "attendances".* FROM "attendances" WHERE "attendances"."id" = 2712 LIMIT 1

Here it actually is not so bad, but it's still sometimes difficult to see what I want. Easy way to suppress the SQL output?

Almswoman answered 29/5, 2013 at 21:28 Comment(1)
See #7759821Emf
S
20

Enter this in the console, or put it in the console's config file:

ActiveRecord::Base.logger = nil
Sarpedon answered 29/5, 2013 at 21:36 Comment(7)
"console's config file": Can you tell me where is this file? I'd like to customize my console a little bitDwanadwane
@MrYoshiji, for pry it's ~/.pryrc. Not sure for irb.Sarpedon
You can also use ~/.irbrc:Eluvium
if defined?(ActiveRecord) && defined?(ActiveRecord::Base) ActiveRecord::Base.logger = nil endEluvium
But this isn't a good solution - it omits all output, not just SQL outputEluvium
Does this only persist until I exit the console session? If not, how do I revert this?Nonobjective
@JeffZivkovic This only persists until the console exits.Sarpedon

© 2022 - 2024 — McMap. All rights reserved.