优化实训搜索

dev_forum
daiao 5 years ago
parent 1520bf941f
commit 3a0e19b48b

@ -45,10 +45,11 @@ class ShixunsController < ApplicationController
if params[:keyword].present?
keyword = params[:keyword].strip
@shixuns = @shixuns.joins(:user, challenges: :challenge_tags).
where("challenge_tags.name like '%#{keyword}%'
or challenges.subject like '%#{keyword}%'
or concat(lastname, firstname) like '%#{keyword}%'
or shixuns.name like '%#{keyword.split(" ").join("%")}%'").distinct
where("challenge_tags.name like :keyword
or challenges.subject like :keyword
or concat(lastname, firstname) like :keyword
or shixuns.name like :name",
keyword: "%#{keyword}%", name: "%#{keyword.split(" ").join("%")}%").distinct
end
## 筛选 状态
@ -66,17 +67,17 @@ class ShixunsController < ApplicationController
bsort = params[:sort] || 'desc'
case params[:order_by] || 'publish_time'
when 'new'
@shixuns = @shixuns.order("shixuns.status = 2 desc, shixuns.created_at #{bsort}")
@shixuns = @shixuns.reorder("shixuns.status = 2 desc, shixuns.created_at #{bsort}")
when 'hot'
@shixuns = @shixuns.order("shixuns.status = 2 desc, myshixuns_count #{bsort}")
@shixuns = @shixuns.reorder("shixuns.status = 2 desc, shixuns.myshixuns_count #{bsort}")
when 'mine'
@shixuns = @shixuns.order("shixuns.created_at #{bsort}")
@shixuns = @shixuns.reorder("shixuns.created_at #{bsort}")
else
@shixuns = @shixuns.order("shixuns.status = 2 desc, publish_time #{bsort}")
@shixuns = @shixuns.reorder("shixuns.status = 2 desc, shixuns.publish_time #{bsort}")
end
@total_count = @shixuns.count
# 用id计数会快10MS左右。
@total_count = @shixuns.count("shixuns.id")
## 分页参数
page = params[:page] || 1

Loading…
Cancel
Save