2016-04-25 7 views
1

少し背景:これは、犬の犬小屋を管理するために設計されたアプリケーションなので、check-inの患者とcheck-outです。 check-inの場合、has_current_stayフィールドはtrueに更新され、逆はcheck-outに発生します。次のように自分のアプリケーションのために、私は、コントローラ内index方法とタイプpatientのモデルを持っているので、.orderフィールドが更新された後にインデックスの順序が変更されない

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

インデックス作業正しくI check-outまで、彼らはトップに浮いた時点で、患者、 has_current_stayフィールドでは、それらが上部に表示されないようにする必要があります。チェックアウト時に何とかインデックスをリフレッシュする必要がありますか?

FWIW:check-outは、患者が関連付けられているstaydestroyと呼ぶことによって達成されます。以下はstays controllerdestroyメソッドです。

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 

ご了承ください。

EDIT: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)はブール型フィールドで昇順ソートを行っています。 を設定しているので、falseはゼロ値のように動作するので、リストの先頭に効果的に移動します。

降順ソート:order(has_current_stay: :desc)を使用して、ユーザが末尾にhas_current_stayの偽の値を保持するようにしてください。

+0

'order(has_current_stay::desc)'は、実際に_with_ 'has_current_stay = true'をリストの一番下に浮かべて、チェックインした人を浮かせて、その下にチェックアウトしています。 。 –

+0

一部のユーザーでは 'has_current_stay'はnilですか?ソートされた結果をどのようにしたいかを詳しく説明できますか? –

+0

'has_current_stay'は決してnilではなく、' false'か 'true'です。私はソートされた結果をリストの上に - > 'has_current_stay = true'とし、それの下に' has_current_stay = false'を単純なものにします。 –

関連する問題