やりたいこと
・先週分の当番表の値をローテンションさせて、それを今週分として保存したい
現状の仕様
・当番表を作成する処理
def create_new_tobanhyo new_tobanhyo = Tobanhyo.where(start_of_period: Time.zone.today.ago(7.days)).map(&:dup) new_tobanhyo_group = new_tobanhyo.group_by(&:fixed) new_roles = new_tobanhyo_group[false].map(&:role_id).rotate new_tobanhyo_group[false].zip(new_roles).each do |hyo, new_role_id| hyo.start_of_period = Time.zone.today.strftime('%Y/%m/%d') hyo.role_id = new_role_id hyo.save! end new_tobanhyo_group[true].each do |hyo| hyo.start_of_period = Time.zone.today.strftime('%Y/%m/%d') hyo.save! end end
・DB情報
ActiveRecord::Schema.define(version: 20180703071547) do create_table "roles", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "rooms", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.boolean "fixed" end create_table "tobanhyos", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| t.integer "room_id" t.integer "role_id" t.boolean "fixed" t.date "start_of_period" t.datetime "created_at", null: false t.datetime "updated_at", null: false end end
やりたいこと
これまでは以下のような処理で当番表を作成していた。
1、1週間前の当番表をコピーする
2、fixedカラムでローテーションさせるroomをグループ分けする
3、ローテーションするroomには、先週のrole_idをローテーションさせた値を入れて保存する
これからは以下のようにしたいので処理を変更したい。
・今週からfixedの値を変更したいroomができた場合に、先週分のレコードを変更することなく対応したい
現状のDB設定で上記の処理を実現するためには、どのようにしたらよろしいでしょうか?
ご回答よろしくお願いいたします!
あなたの回答
tips
プレビュー