class Ecs::LinkCourseService < ApplicationService Error = Class.new(StandardError) attr_reader :ec_course, :params def initialize(ec_course, params) @ec_course = ec_course @params = params end def call course = Course.find_by(id: params[:course_id]) raise Error, '课程不存在' if course.blank? ec_course.ec_major_courses.create!(course: course) end end