5

我經常需要與rails c一起去Rails控制檯。然後我運行一些循環遍歷模型記錄的語句。我需要輸出信息,但是所有的SQL代碼也都是亂七八糟的。像:簡單的方法來抑制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輸出?

+2

見http://stackoverflow.com/questions/7759321/disable-rails-3-1-sql-logging – eugen

回答

10

在控制檯輸入以下,或把它在控制檯的配置文件:

ActiveRecord::Base.logger = nil 
+0

「控制檯的配置文件」:你能告訴我這個文件在哪裏嗎?我想定製我的控制檯 – MrYoshiji

+0

@MrYoshiji,因爲它是'〜/ .pryrc'。不確定irb。 – Mori