Мы создаем приложение для планирования в классе и пытаемся создать функцию, позволяющую копировать предыдущую смену и всех сотрудников этой смены на следующую неделю планирования.Вот мой код на данный момент:
def copy
set_calendar
@past_shift = Shift.find(params[:id])
if @calendar.users.owners.include?(current_user) || @calendar.users.managers.include?(current_user)
@shift = @past_shift.clone
if @shift.save
render json: ("Shift copied successfully"), status: :ok
else
render json: @usershift.errors, status: :uprocessable_entity
end
else
render json: ("You do not have access to copy shifts"), status: :unauthorized
end
end
Я использовал и .clone, и .dup, но, похоже, он копирует только смены, не копируя сотрудников, связанных с этими сменами.Мы будем благодарны за любую помощь или руководство по правильному пути.
Ниже приведен весь контроллер для смен:
class ShiftsController < ApplicationController
def index
set_calendar
if params["start_date"] && params["end_date"]
start_date = params["start_date"].to_date
end_date = params["end_date"].to_date
if @calendar.users.include?(current_user)
@shifts = Shift.where(
calendar_id: @calendar.id,
start_time: start_date.beginning_of_day .. end_date.end_of_day)
render "/shifts/index.json", status: :ok
else
render json: '{}', status: :unauthorized
end
else
render json: {'error': 'start date and end date are required'}, status: :unprocessable_entity
end
end
def myschedule
@user = User.find(params[:user_id])
@publishedshifts = @user.shifts.where(published: true)
if @user
render "/shifts/index2.json", status: :ok
else
render json: ('You do not have access to these shifts'), status: :unauthorized
end
end
def create
set_calendar
if @calendar.users.owners.include?(current_user) || @calendar.users.managers.include?(current_user)
@shift = Shift.new(shift_params)
if @shift.save
render "/shifts/create.json", status: :ok
else
render json: @shift.errors
end
else render json: ('You do not have access to create a shift.'), status: :unauthorized
end
end
def copy
set_calendar
@past_shift = Shift.find(params[:id])
if @calendar.users.owners.include?(current_user) || @calendar.users.managers.include?(current_user)
@shift = @past_shift.clone
if @shift.save
render json: ("Shift copied successfully"), status: :ok
else
render json: @usershift.errors, status: :uprocessable_entity
end
else
render json: ("You do not have access to copy shifts"), status: :unauthorized
end
end
def update
set_calendar
set_shift
if @calendar.users.owners.include?(current_user) || @calendar.users.managers.include?(current_user)
if @shift.update_attributes(shift_params)
render "/shifts/update.json", status: :ok
else
render json: @shift.errors, status: :uprocessable_entity
end
else render json: ("You don't have access to update shifts."), status: :unauthorized
end
end
def destroy
set_calendar
set_shift
if @calendar.users.owners.include?(current_user) || @calendar.users.managers.include?(current_user)
if @shift.destroy
render json: ("Shift deleted!"), status: :ok
else
render json: @shift.errors, status: :uprocessable_entity
end
else render json: ("You don't have access to delete shifts.")
end
end
private
def set_calendar
@calendar = Calendar.find(params[:calendar_id])
end
def set_shift
@shift = Shift.find(params[:id])
end
def shift_params
params.permit(:start_time, :end_time, :calendar_id, :capacity, :published)
end
end
Схема:
create_table "users", force: :cascade do |t|
t.string "email"
t.string "phone_number"
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "api_token"
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
end
create_table "usershifts", force: :cascade do |t|
t.bigint "user_id"
t.bigint "shift_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["shift_id"], name: "index_usershifts_on_shift_id"
t.index ["user_id"], name: "index_usershifts_on_user_id"
end
create_table "shifts", force: :cascade do |t|
t.datetime "start_time"
t.datetime "end_time"
t.bigint "calendar_id"
t.integer "capacity"
t.boolean "published"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["calendar_id"], name: "index_shifts_on_calendar_id"
end