class Ecs::SaveGraduationCourseSupportsService < ApplicationService include AcceptsNestedAttributesHelper attr_reader :graduation_subitem, :params def initialize(graduation_subitem, params) @params = params @graduation_subitem = graduation_subitem end def call set_course_supports_position! Ecs::SaveGraduationCourseSupportForm.new(params).validate! accepts_attributes = build_accepts_nested_attributes( graduation_subitem, graduation_subitem.ec_course_supports, params[:course_supports] ) graduation_subitem.assign_attributes(ec_course_supports_attributes: accepts_attributes) graduation_subitem.save! graduation_subitem end def set_course_supports_position! params[:course_supports].each_with_index do |item, index| item[:position] = index + 1 end end end