2016-04-25 55 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'在它下面 - 这很简单。 –