ホテルの予約システムを実装しています。
ruby
1# 予約テーブル 2class Reservation < ActiveRecord::Base 3 belongs_to :plan, inverse_of: :reservations 4 delegate :room, to: :plan, allow_nil: true 5end 6 7# 予約プランテーブル 8class Plan < ActiveRecord::Base 9 has_many :reservations, inverse_of: :plan 10 belongs_to :room, inverse_of: :plans 11end 12 13# 部屋テーブル 14class Room < ActiveRecord::Base 15 has_many :plans, inverse_of: :room 16 has_many :reservations, through: :plans 17end
このような関連の時、[ある部屋の予約状況を知りたい]時
ReservationsController
では
ruby
1@reservation = Room.find(params[:id]).reservations
のようにしておくと 中間テーブルの役割を果たすplans
が中でJoinされて
[予約プランが一つでもないと予約状況が参照できない]状態となります。
そのため reservations
にroom_id
を追加して直接参照したいのですが
plans
にもroom_id
があるためDB設計的に冗長なのではと感じてます。
この場合、どのように設計をすれば良いでしょうか。
あなたの回答
tips
プレビュー