优化实训搜索

dev_forum
daiao 5 years ago
parent 1520bf941f
commit 3a0e19b48b

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

Loading…
Cancel
Save