课程首页搜索优化

dev_course
daiao 6 years ago
parent 629e9461ea
commit 544152cdf6

@ -77,11 +77,17 @@ class CoursesController < ApplicationController
# 根据搜索关键字进一步筛选
if params[:search].present?
# REDO:Extension
user_ids = User.includes(user_extension: :school).where("schools.name like ?", "%#{params[:search]}%").pluck(:id)
course_ids = CourseMember.includes(:user, :course).where("course_members.course_id in (?) and course_members.role in (1,2,3)
and CONCAT(users.lastname, users.firstname) like ?", @courses.map(&:id), "%#{params[:search]}%")
.pluck(:course_id)
@courses = @courses.where("name like ?", "%#{params[:search]}%").or(@courses.where(tea_id: user_ids)).or(@courses.where(id: course_ids))
#user_ids = User.includes(user_extension: :school).where("schools.name like ?", "%#{params[:search]}%").pluck(:id)
#course_ids = CourseMember.includes(:user, :course).where("course_members.course_id in (?) and course_members.role in (1,2,3)
# and CONCAT(users.lastname, users.firstname) like ?", @courses.map(&:id), "%#{params[:search]}%")
# .pluck(:course_id)
#@courses = @courses.where("name like ?", "%#{params[:search]}%").or(@courses.where(tea_id: user_ids)).or(@courses.where(id: course_ids))
# 6:21 daiao
sql = %Q{
(course_members.role in (1,2,3) and CONCAT(users.lastname, users.firstname) like :keyword) or courses.name like :keyword
or schools.name like :keyword
}
@courses.includes(course_members: [user: [user_extension: :school]]).where(sql, keyword: "%#{params[:search]}%")
end
@courses_count = @courses.size

Loading…
Cancel
Save