在Rails控制台中轻松抑制SQL输出的方法?

时间:2013-05-29 21:28:55

标签: ruby-on-rails ruby-on-rails-3 irb ruby-on-rails-4 rails-console

我经常需要使用rails c继续使用Rails控制台。然后我运行一些循环遍历模型记录的语句。我需要输出信息,但所有的SQL代码都会被遍布。像:

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

我把nil放在最后,所以我没有得到所有学生的丑陋。这是输出:

  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

这实际上并没有那么糟糕,但有时仍然很难看到我想要的东西。简单的方法来抑制SQL输出?

1 个答案:

答案 0 :(得分:12)

在控制台中输入,或将其放在控制台的配置文件中:

ActiveRecord::Base.logger = nil