From 49d0d214def26c6ec79ba64b932e124314a008b3 Mon Sep 17 00:00:00 2001 From: huang Date: Sun, 24 Jan 2016 08:48:38 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=B2=BE=E5=93=81=E8=AF=BE=E7=A8=8B?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/helpers/courses_helper.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/helpers/courses_helper.rb b/app/helpers/courses_helper.rb index fb7214fce..2d2d83174 100644 --- a/app/helpers/courses_helper.rb +++ b/app/helpers/courses_helper.rb @@ -40,9 +40,14 @@ module CoursesHelper a_courses << c.id end a_courses << a_tags unless a_tags.length == 0 + ids = a_courses.flatten.uniq + ids_count = ids.length + limit = 5 - ids_count.to_i + sql = "SELECT distinct c.id FROM course_activities cs, courses c where cs.course_id = c.id and c.is_excellent =1 order by cs.updated_at desc;" + default_ecourse = Course.find_by_sql(sql).where("id is not in (?)", a_courses.ids) + arr_result = ids << default_ecourse # 过滤条件:精品课程、本身不在搜索范围 - e_courses = Course.where("is_excellent =? and id in (?)",1,a_courses.flatten.uniq).where("id !=?",current_course.id) - e_courses + e_courses = Course.where("is_excellent =? and id in (?)",1,).where("id !=?", arr_result, current_course.id) end # 判断精品课程是否可见,非课程成员无法查看私有课程 From 154417378ef3957e4e0d7ed6a8cebb1c02082897 Mon Sep 17 00:00:00 2001 From: huang Date: Sun, 24 Jan 2016 10:58:58 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=B2=BE=E5=93=81=E8=AF=BE=E7=A8=8B?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E6=8E=A8=E8=8D=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/helpers/courses_helper.rb | 32 +++++++++++------- app/views/courses/_recommendation.html.erb | 38 ++++++++++++---------- 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/app/helpers/courses_helper.rb b/app/helpers/courses_helper.rb index 2d2d83174..4e90fc106 100644 --- a/app/helpers/courses_helper.rb +++ b/app/helpers/courses_helper.rb @@ -35,19 +35,29 @@ module CoursesHelper # a_tags = Course.find_by_sql(sql).select{|course| course.is_public ==1 unless User.current.member_of_course?(course)} # 通过elastic结果获取精品课程 a_courses = [] - courses = Course.search(keywords) - courses.each do |c| - a_courses << c.id - end + #courses = Course.search(keywords) + #courses.each do |c| + # a_courses << c.id + #end a_courses << a_tags unless a_tags.length == 0 - ids = a_courses.flatten.uniq - ids_count = ids.length - limit = 5 - ids_count.to_i - sql = "SELECT distinct c.id FROM course_activities cs, courses c where cs.course_id = c.id and c.is_excellent =1 order by cs.updated_at desc;" - default_ecourse = Course.find_by_sql(sql).where("id is not in (?)", a_courses.ids) - arr_result = ids << default_ecourse + # 课程本身不能搜索显示自己 + excellent_ids = a_courses.flatten.uniq.delete_if{|i| i == current_course.id} + limit = 5 - excellent_ids.length.to_i + sql = "SELECT distinct c.id FROM course_activities cs, courses c where cs.course_id = c.id + and c.is_excellent =1 and c.id != #{current_course.id} order by cs.updated_at desc;" + default_ecourse_ids = Course.find_by_sql(sql).flatten + # REDO:时间紧,待优化 + default_ids =[] + default_ecourse_ids.each do |de| + default_ids << de.id + end + default_ids = default_ids - excellent_ids + #default_ecourse = Course.where("id is not in (?)", ids).find_by_sql(sql).flatten.delete_if{|i| i == current_course.id}.flatten + arr_result = excellent_ids << default_ids + arr_result = arr_result.flatten.first(5) + return arr_result # 过滤条件:精品课程、本身不在搜索范围 - e_courses = Course.where("is_excellent =? and id in (?)",1,).where("id !=?", arr_result, current_course.id) + #e_courses = Course.where("is_excellent =? and id in (?)",1, arr_result).where("id !=?", current_course.id) end # 判断精品课程是否可见,非课程成员无法查看私有课程 diff --git a/app/views/courses/_recommendation.html.erb b/app/views/courses/_recommendation.html.erb index d0afc6ebd..2b890ca5d 100644 --- a/app/views/courses/_recommendation.html.erb +++ b/app/views/courses/_recommendation.html.erb @@ -1,18 +1,20 @@ -<% unless excellent_course_recommend(course).count == 0 %> - -<% end %> - +<% unless excellent_course_recommend(course).length == 0 %> + +<% end %> +