有点背景:这是一个设计用于管理狗窝的应用程序,因此您可以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查询 –