2016-04-25 56 views
1

有點背景:這是一個設計用於管理狗窩的應用程序,因此您可以check-in患者和check-out。在check-in時,has_current_stay字段更新爲true,相反發生在check-out索引排序後更改order字段

所以對於我的應用程序有patient類型的與控制器的index方法的模型如下:

def index 
    @patients = Patient.search(params[:search]).order(:has_current_stay) 
    if @patients.count == 1 
     redirect_to @patients.first 
    end 
end 

索引正常工作,直到我check-out患者,他們浮到頂部在該點的名單 - 即使他們has_current_stay領域應該防止他們在頂部。我需要在退房時以某種方式「刷新」索引嗎?

FWIW:check-out通過調用stay上與患者有關的destroy來完成。以下是stays controller中的destroy方法。

def destroy 

    @stay = Stay.find(params[:id]).destroy 

    @runn = Runn.find_by_id(@stay.runn_id) 
    @runn.occupied = false 
    @runn.save 

    @patient = Patient.find(@stay.patient_id) 
    @patient.has_current_stay = false 
    @patient.save 



    flash[:success] = "Checked out #{@patient.name}" 
    redirect_to patients_url 
    end 

任何指導表示讚賞。

編輯:SQL查詢時patients#index要求:

Started GET "/all_patients" for ::1 at 2016-04-25 15:26:04 -0500 
    ActiveRecord::SchemaMigration Load (0.5ms) SELECT "schema_migrations".* FROM "schema_migrations" 
Processing by PatientsController#index as HTML 
    User Load (1.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 3]] 
    (0.8ms) SELECT COUNT(*) FROM "patients" 
    Rendered shared/_search_an_index.html.erb (0.7ms) 
    Patient Load (1.3ms) SELECT "patients".* FROM "patients" ORDER BY "patients"."has_current_stay" DESC 
    CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 3]] 
    Stay Load (0.6ms) SELECT "stays".* FROM "stays" WHERE "stays"."patient_id" = $1 ORDER BY "stays"."id" DESC LIMIT 1 [["patient_id", 2]] 
    Runn Load (0.6ms) SELECT "runns".* FROM "runns" WHERE "runns"."id" = $1 ORDER BY ident ASC LIMIT 1 [["id", 1]] 
    CACHE (0.0ms) SELECT "stays".* FROM "stays" WHERE "stays"."patient_id" = $1 ORDER BY "stays"."id" DESC LIMIT 1 [["patient_id", 2]] 
    CACHE (0.0ms) SELECT "runns".* FROM "runns" WHERE "runns"."id" = $1 ORDER BY ident ASC LIMIT 1 [["id", 1]] 
    Stay Load (0.4ms) SELECT "stays".* FROM "stays" WHERE "stays"."patient_id" = $1 ORDER BY "stays"."id" DESC LIMIT 1 [["patient_id", 35]] 
    Runn Load (0.3ms) SELECT "runns".* FROM "runns" WHERE "runns"."id" = $1 ORDER BY ident ASC LIMIT 1 [["id", 2]] 
    CACHE (0.0ms) SELECT "stays".* FROM "stays" WHERE "stays"."patient_id" = $1 ORDER BY "stays"."id" DESC LIMIT 1 [["patient_id", 35]] 
    CACHE (0.0ms) SELECT "runns".* FROM "runns" WHERE "runns"."id" = $1 ORDER BY ident ASC LIMIT 1 [["id", 2]] 
    Rendered patients/index.html.erb within layouts/application (158.0ms) 
    Rendered layouts/_shim.html.erb (0.3ms) 
    Rendered layouts/_header.html.erb (2.4ms) 
Completed 200 OK in 643ms (Views: 560.0ms | ActiveRecord: 18.5ms) 
+0

是否在Patient.search範圍中使用了任何順序?您是否可以在'Patient'模型的默認範圍內使用排序?因爲如果沒有的話,我希望安東尼的答案能奏效。您可以向我們展示在'index'操作中進行搜索時運行的SQL查詢嗎? – BoraMa

+0

我不知道爲什麼你的desc排序不會工作,這裏是類似的示例,其中順序運作良好:http://stackoverflow.com/questions/12524311/how-to-order-results-by-an-existing-boolean -attribute-first –

+0

@BoraMa我追加了我的SQL查詢 –

回答

0

.order(:has_current_stay)上一個布爾字段做升序排序。由於您正在設置@patient.has_current_stay = false,因此false會像零值一樣起作用,因此可以將其有效地置於列表的首位。

嘗試做降序排列:order(has_current_stay: :desc),以保持用戶在底部爲has_current_stay的虛假值。

+0

'order(has_current_stay::desc)'實際上將人員帶有'has_current_stay = true'浮動到列表的底部,然後浮動簽入的人員然後在他們之下籤出 - 所以基本上是我之前所做的反對。 –

+0

某些用戶的'has_current_stay'爲零嗎?你能詳細說明你想要的排序結果是什麼嗎? –

+0

'has_current_stay'永遠不是零,它可以是'false'或'true'。我希望排序後的結果是 - >'has_current_stay = true'在列表的頂部,'has_current_stay = false'在它下面 - 這很簡單。 –