有點背景:這是一個設計用於管理狗窩的應用程序,因此您可以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)
是否在Patient.search範圍中使用了任何順序?您是否可以在'Patient'模型的默認範圍內使用排序?因爲如果沒有的話,我希望安東尼的答案能奏效。您可以向我們展示在'index'操作中進行搜索時運行的SQL查詢嗎? – BoraMa
我不知道爲什麼你的desc排序不會工作,這裏是類似的示例,其中順序運作良好:http://stackoverflow.com/questions/12524311/how-to-order-results-by-an-existing-boolean -attribute-first –
@BoraMa我追加了我的SQL查詢 –