2015-09-17 51 views
0

我們正在嘗試加載與AR包含查詢的關聯,但我們遇到了問題。Rails 4 ActiveRecord幫助使用包含查詢與Postgres

控制器中的相關位:

def index 
    @students = current_admin.current_cohort.students.includes(:attendance_records) 
end 

這部分似乎好工作與結果查詢:

Student Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."type" IN ('Student') AND "users"."id" IN (413, 400, 386, 399, 380, 410) ORDER BY "users"."name" ASC 
    Student Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."type" IN ('Student') AND "users"."cohort_id" = $1 ORDER BY "users"."name" ASC [["cohort_id", 8]] 
    AttendanceRecord Load (4.2ms) SELECT "attendance_records".* FROM "attendance_records" WHERE "attendance_records"."student_id" IN (212, 216, 210, 282, 305, 283, 361, 265, 168, 229, 228, 376, 338, 223, 339, 308, 214, 185, 165, 224, 211, 91, 172, 221, 266, 220, 192, 240, 173, 226, 218, 302, 183, 241, 163, 219, 243, 231, 272, 244, 362, 213, 328, 171, 227, 202, 201, 275, 248, 236, 342, 217, 293, 194, 208, 39, 199, 170, 284) 

的問題是,我們的考勤記錄其他地方的檢查如下所示。這部分內容是查詢數據庫中的考勤記錄,即使它們已經預加載,導致n + 1查詢問題。

查看代碼:

<% if student.signed_in_today? %> 
    <p class="text-success"><span class="check">&#x2713;</span> Signed in</p> 
    <% if student.signed_out_today? %> 
     <p class="text-success"><span class="check">&#x2713;</span> Signed out</p> 
    <% else %> 
     <%= form_for student.attendance_records.today.first do |f| %> 
     <%= f.hidden_field :student_id, value: student.id %> 
     <%= f.hidden_field :signing_out, value: true %> 
     <%= f.submit "I'm leaving", class: 'btn btn-warning' %> 
     <% end %> 
    <% end %> 
<% else %> 
    <%= form_for AttendanceRecord.new do |f| %> 
    <%= f.hidden_field :student_id, value: student.id %> 
    <%= f.submit "I'm soloing", class: 'btn btn-primary' %> 
    <% end %> 
<% end %> 

型號代碼:

# Student model 
    def signed_in_today? 
    attendance_records.today.exists? 
    end 

    def signed_out_today? 
    if signed_in_today? 
     attendance_records.today.first.signed_out_time != nil 
    end 
    end 

# AttendanceRecord model 
    scope :today, -> { where(date: Time.zone.now.to_date) } 

有問題的查詢是以下:

AttendanceRecord Exists (0.2ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 212], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 216], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 210], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 282], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 305], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 283], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 361], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 265], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 168], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.2ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 229], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.2ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 228], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 376], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 338], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.2ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 223], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 339], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 308], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 214], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 185], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 165], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 224], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 211], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.2ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 91], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.2ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 172], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.2ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 221], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.2ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 266], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 220], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 192], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 240], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.3ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 173], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.2ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 226], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.2ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 218], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.2ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 302], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.2ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 183], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.2ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 241], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.2ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 163], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.2ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 219], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.2ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 243], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 231], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 272], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 244], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 362], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 213], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 328], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 171], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.2ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 227], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 202], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 201], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.2ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 275], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.2ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 248], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 236], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 342], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.2ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 217], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 293], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 194], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 208], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.3ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 39], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 199], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 170], ["date", "2015-09-17"]] 
    AttendanceRecord Exists (0.1ms) SELECT 1 AS one FROM "attendance_records" WHERE "attendance_records"."student_id" = $1 AND "attendance_records"."date" = $2 LIMIT 1 [["student_id", 284], ["date", "2015-09-17"]] 

任何幫助將不勝感激。謝謝!

+0

student.attendance_records.today,今天**正在導致額外的查詢,基本上你在這裏需要的是一個連接,將獲取今天的所有出勤記錄。 – Zahid

回答

0

只要您致電where(就像您在今天的作用域中那樣),您將觸發查詢。你用你的學生索引查詢得到你需要的記錄 - 它們全都在內存中,所以你應該把它當作一個數組 - 使用數組方法如select找到你想要的記錄。

+0

非常感謝!我最終從signed_in_today刪除了今天的範圍調用?方法,並像上面提到的那樣通過'select'循環遍歷:'attendance_records.select {| attendance_record | attendance_record.date == Date.today} .any?'。我們仍在尋找另一種方式來處理這個問題,但這樣更有效率。 –