代码优化

dev_course
daiao 6 years ago
parent d094259051
commit 130a39ed5b

@ -165,7 +165,7 @@ class Course < ApplicationRecord
all_course_module_types.each do |type| all_course_module_types.each do |type|
hidden_value = course_module_types.include?(type) ? 0 : 1 hidden_value = course_module_types.include?(type) ? 0 : 1
course_module = get_course_module_by_type(type, self.id) course_module = course_modules.where(module_type: type).first
course_module.update_attribute(:hidden, hidden_value) if course_module.present? course_module.update_attribute(:hidden, hidden_value) if course_module.present?
end end
end end
@ -174,8 +174,9 @@ class Course < ApplicationRecord
%w[activity shixun_homework common_homework group_homework graduation exercise poll attachment board course_group] %w[activity shixun_homework common_homework group_homework graduation exercise poll attachment board course_group]
end end
def get_course_module_by_type(type, course_id) def get_course_module_by_type(type)
CourseModule.where(course_id: course_id, module_type: type).first #CourseModule.where(course_id: course_id, module_type: type).first
self.course_modules.where(module_type: type).first
end end
# 创建课程讨论区 # 创建课程讨论区

@ -170,7 +170,7 @@ class HomeworkCommon < ApplicationRecord
#删除时更新题库中的引用数 #删除时更新题库中的引用数
def update_homework_bank_quotes def update_homework_bank_quotes
old_banks = HomeworkBank.where(homework_common_id: self.id) old_banks = self.homework_bank
unless old_banks.blank? unless old_banks.blank?
old_banks.each do |bank| old_banks.each do |bank|
bank.update_attributes(quotes: (bank.quotes - 1) > 0 ? (bank.quotes - 1) : 0, homework_common_id: nil) bank.update_attributes(quotes: (bank.quotes - 1) > 0 ? (bank.quotes - 1) : 0, homework_common_id: nil)
@ -243,10 +243,15 @@ class HomeworkCommon < ApplicationRecord
end end
def min_group_publish_time def min_group_publish_time
HomeworkGroupSetting.where("homework_common_id = #{self.id} and publish_time is not null").pluck(:publish_time).min #HomeworkGroupSetting.where("homework_common_id = #{self.id} and publish_time is not null").pluck(:publish_time).min
# 可以使用includes
self.homework_group_settings.where("publish_time is not null").pluck(:publish_time).min
end end
def max_group_end_time def max_group_end_time
HomeworkGroupSetting.where("homework_common_id = #{self.id} and end_time is not null").pluck(:end_time).max #HomeworkGroupSetting.where("homework_common_id = #{self.id} and end_time is not null").pluck(:end_time).max
# 可以使用includes
self.homework_group_settings.where("end_time is not null").pluck(:end_time).max
end end
end end

Loading…
Cancel
Save