9

我经常需要在 Rails 控制台上使用rails c. 然后我运行一些循环遍历模型记录的语句。我需要输出信息,但所有的 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 输出的简单方法?

4

1 回答 1

17

在控制台中输入这个,或者把它放在控制台的配置文件中:

ActiveRecord::Base.logger = nil
于 2013-05-29T21:36:35.083 回答