私はRuby on Railsホステルベッド予約システムを構築しています。可用性カレンダーを読み込むためのより良い方法を探しています。以下のコードは、ベッドの空き状況(添付の画像を参照)を示すのに最適ですが、複数のWHEREデータベース呼び出しがサーバーに負担をかけすぎているため、データを処理するための迅速な方法が必要です。レンタル可用性のためのデータベース利用可能日カレンダー
モデル:
user.rb
bed.rb(user_idは、名)
guest.rb(user_idは、名前、電話番号、住所、電子メール)
予約.rb(ユーザーID、ベッドID、ゲストID、日付)
ユーザーは、ホステルの所有者です。彼らは彼らのビジネスと同じ数のベッドを作成し、それらに名前を付けます(ベッド#1、ベッド#2、ツイン・アトリック、マスター2階など)。
各ユーザーには多くのゲストがいて、特定の日付にホステルで寝る人です。
simple_calendarの宝石を使用して、私は今月のすべての予約を取得し、週間表示を表示するように処理します。
ソートロジック:
ステップ週の最初の日のためにCURRENT_USERのベッドを通じ1.反復(simple_calendar用品毎日の予約)
ステップ2.ベッドが持っている場合。その日の予約(!= nil)、ベッド名とその予約のゲスト名を入れてください。
手順3.ベッドが予約されていない場合は、ベッド名を入力し、「空」を入力します。週の最初の日を完了した後
ステップ4、翌日繰り返しに移動する。(simple_calendarは、このの世話をする)
私が言ったように、作品以下のコードが、ユーザーは50を持っていますベッドは、それは毎日50コール、7日間のカレンダーの350(とそれ以上の場合は、ゲストの名前を見つける必要があります)を呼び出します。
詳細情報や説明が必要な場合はお知らせください。ありがとう!
= week_calendar events: @monthly_reservations, attribute: :date do |date, appts|
- current_user.beds.each do |bed|
- if bed.reservations.where(date: date).first != nil
- guest_name = bed.reservations.where(date: date).first.guest.abbreviated_name
%p{style: "border: 1px solid black; padding: 5px; font-size: 10px;"}
= "#{bed.name}: #{guest_name}"
- else
%p{style: "border: 1px solid black; padding: 5px; font-size: 10px;"}
= "#{bed.name}: Empty"
CURRENT Rails hostel bed reservation system
EDIT:しかし、問題は、とベッドの名のラインナップを作っている
IDEAL Rails bed reservation system
: 理想的なカレンダーのレイアウトは以下の画像のようになります。正しいベッド予約。 5月2日に2つの予約(ベッド1とベッド4)しかない場合、コードはベッド4の予約を最後に挿入する前に2人のEMPTYSを残すことを知らない。理にかなっている?
EDIT#2:
@DavidAldridge、このような何か:このような
何か?
reservs = {"[2017-05-01, bed_1]" => "guest_23", "[2017-05-01, bed_2]" => "empty"}
EDITここ3 ログはわずか5ベッドホステルをロードするときにどのように見えるかです。
Bed Load (0.3ms) SELECT "beds".* FROM "beds" WHERE "beds"."user_id" = $1 [["user_id", 1]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 1], ["date", "2017-07-02"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 2], ["date", "2017-07-02"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 3], ["date", "2017-07-02"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 4], ["date", "2017-07-02"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 5], ["date", "2017-07-02"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 1], ["date", "2017-07-03"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 2], ["date", "2017-07-03"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 3], ["date", "2017-07-03"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 4], ["date", "2017-07-03"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 5], ["date", "2017-07-03"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 1], ["date", "2017-07-04"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 2], ["date", "2017-07-04"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 3], ["date", "2017-07-04"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 4], ["date", "2017-07-04"]]
Reservations Load (0.1ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 5], ["date", "2017-07-04"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 1], ["date", "2017-07-05"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 2], ["date", "2017-07-05"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 3], ["date", "2017-07-05"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 4], ["date", "2017-07-05"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 5], ["date", "2017-07-05"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 1], ["date", "2017-07-06"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 2], ["date", "2017-07-06"]]
Reservations Load (0.1ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 3], ["date", "2017-07-06"]]
Reservations Load (0.1ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 4], ["date", "2017-07-06"]]
Reservations Load (0.3ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 5], ["date", "2017-07-06"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 1], ["date", "2017-07-07"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 2], ["date", "2017-07-07"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 3], ["date", "2017-07-07"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 4], ["date", "2017-07-07"]]
Reservations Load (0.1ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 5], ["date", "2017-07-07"]]
Reservations Load (0.2ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 1], ["date", "2017-07-08"]]
Reservations Load (0.6ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 2], ["date", "2017-07-08"]]
Reservations Load (0.3ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 3], ["date", "2017-07-08"]]
Reservations Load (0.3ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 4], ["date", "2017-07-08"]]
Reservations Load (0.3ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."bed_id" = $1 AND "reservations"."date" = $2 ORDER BY "reservations"."id" ASC LIMIT 1 [["bed_id", 5], ["date", "2017-07-08"]]
@DavidAldridgeの提案に続きEDIT 4 、私は、コントローラ内のハッシュを作成し、私の意見にそれを渡されました。結果はSTELLARです!
コントローラコード:
@reservations = {}
@monthly_reservations.each do |x|
@reservations[[x.date,x.bed_id]] = x.guest.try(:name)
end
、ビューコード:
= week_calendar events: @monthly_reservations, attribute: :date do |date, appts|
- current_user.beds.each do |bed|
- if @reservations[[date, bed.id]] != nil
%p{style: "border: 1px solid black; padding: 5px; font-size: 10px;"}
= "#{bed.name}: #{@reservations[[date, kennel.id]]}"
- else
%p{style: "border: 1px solid black; padding: 5px; font-size: 10px;"}
= "#{bed.name}:"
「私は、データベース呼び出しがあまりにもサーバー上で課税されている複数のように感じます」。これは直感であり、正しいかもしれない。見つかる唯一の方法は、 'log/development.log'をよく調べて、どのようにクエリが実行されているかを確認することです。一般的には、 'tail -f log/development.log'をウィンドウ*内に常時開いたままにして、ページを読み込んで機能を開発するときのコードの効率性や醜さを感じることをお勧めします。 – tadman
今月のcurrent_userのすべてのレコードを取得し、結果を繰り返し処理するだけで、テーブルを一度クエリするのはなぜですか? – RToyo
これはまさに@monthly_reservations変数に含まれるもので、その月のすべてのcurrent_userの予約です。 しかし、私はホステルが持っているすべてのベッドを表示する必要があり、誰が誰であれ、誰がベッドにとどまっているのかを表示する必要があるので、どこに来るのですか。 Tadman、私は直感的な部分について完全に同意します。たぶんそれはサーバーには負担をかけるものではないかもしれませんが、ログを見てページが読み込まれるたびにWHEREが決して終わらないことを見ると、私は心配しています。 –