我正在通过以下业务流程实施一个新应用:
创建公司的最佳方法是什么?
第一个解决方案:创建了TrainingCompany的新服务
class TrainingCompanyService
def initialize(company_name)
@name = company_name
end
def create_new_training_company
TrainingCompany.new(company_name: @name).save
end
end
在RegistrationFormController中创建动作:
def create
@registration_form = RegistrationForm.new(registration_form_params)
respond_to do |format|
if @registration_form.save
format.html { redirect_to @registration_form, notice: 'Registration form was successfully created.' }
TrainingCompanyService.new(@registration_form.company_name).create_new_training_company
RegistrationFormMailer.with(registration_form: @registration_form).after_registration_email.deliver_later
else
format.html { render :new }
end
end
end
第二个解决方案:TrainingCompany模型中的新方法:
class RegistrationForm < ApplicationRecord
belongs_to :training_company, optional: true
has_one_attached :registration_form
has_secure_token :signed_form_upload_token
def create_new_training_company
TrainingCompany.new(company_name: self.company_name, registration_form_id: self.id).save
end
end
在RegistrationFormController中创建动作:
def create
@registration_form = RegistrationForm.new(registration_form_params)
respond_to do |format|
if @registration_form.save && @registration_form.create_new_training_company
format.html { redirect_to @registration_form, notice: 'Registration form was successfully created.' }
RegistrationFormMailer.with(registration_form: @registration_form).after_registration_email.deliver_later
else
format.html { render :new }
end
end
end
您会选择哪种解决方案,为什么?我个人更喜欢第二种方法(模型中的新方法)...