我有一个工作人员类,它删除了早于InquiryProcess
的{{1}}(默认值应设置为6个月)。潜在的数据量可能很大,因此是否有机会通过下面的代码加快删除速度?
x time
也许我可以在某个地方使用class OldProcessRemover
def initialize(date: 6.months.ago)
@date = date
end
attr_reader :date
def call
remove_loan
remove_checking_account
end
private
def remove_loan
loan_template = InquiryTemplate.find_by(inquiry_process_name: InquiryTemplate::LOAN_APPLICATION_PROCESS_NAME)
loan_template.inquiry_processes.where('created_at <= ?', date).each(&:destroy)
end
def remove_checking_account
checking_account_template = InquiryTemplate.find_by(
inquiry_process_name: InquiryTemplate::CHECKING_ACCOUNT_OPENING_PROCESS_NAME,
)
checking_account_template.inquiry_processes.where('created_at <= ?', date).each(&:destroy)
end
end
吗?我认为这些方法不是单一责任,因此重构也将有所帮助。
答案 0 :(得分:0)
class OldProcessRemover
def initialize(date: 6.months.ago)
@date = date
end
attr_reader :date
def call
remove_loan
remove_checking_account
end
private
def remove_loan
remove_processes!(InquiryTemplate::LOAN_APPLICATION_PROCESS_NAME)
end
def remove_checking_account
remove_processes!(InquiryTemplate::CHECKING_ACCOUNT_OPENING_PROCESS_NAME)
end
def remove_processes!(process_name)
account_template = InquiryTemplate.find_by(
inquiry_process_name: process_name
)
account_template.inquiry_processes
.where('created_at <= ?', date)
.find_in_batches { |group| group.destroy_all }
end
end
我认为在这里使用.find_in_batches { |group| group.destroy_all }
或.find_each {|record| record.destroy }
之间没有什么主要区别。