新手问题 怎么把传到控制器的检索条件给写进语句里?

yu7272yu · 2016年11月01日 · 最后由 yu7272yu 回复于 2016年11月03日 · 1879 次阅读
ef index_data
    qc = params[:q]
    qc.merge!({"order_total_"+params[:order_total] => params[:order_total_cont]}) if params[:order_total].present? and params[:order_total_cont].present? 

        @q = StatisticalChargingPile.search(qc).result.select("sum(order_total) as order_total_sum, parking_area_name, third_party_pile_id, serial_number, operator_name").group("serial_number").having("order_total_sum > ? ",params[:order_total_cont] ).order("created_at desc")

end
2 楼 已删除

@hz_qiuyuanxin 传来的

params[:order_total] 

是个 (gt,gteq,lt,lteq),想转换成符号(>,>=,<,<=),写进 having 里面去

#3 楼 @yu7272yu


params[:order_total] = case params[:order_total]
when 'gt'
 '>'
when 'gteq'
 '>='
when 'lt'
 '<'
when 'lteq'
 '<='
else
 '='
end


...
.having("order_total_sum #{params[:order_total]} ? ", params[:order_total_cont] )
...

@mimosa 谢谢了,这样写的是

hs = {"gt" => ">", "gteq" => ">=", "lt" => "<", "lteq" => "<="}
        constr1 = "order_total_sum #{hs[params[:order_total]]} #{params[:order_total_cont]} and " if params[:order_total].present? and params[:order_total_cont].present?
cs = "#{constr1} 1=1"
@q = StatisticalChargingPile.search(qc).result.select("sum(order_total) as order_total_sum, parking_area_name, third_party_pile_id, serial_number, operator_name").group("serial_number").having(cs ).order("created_at desc")

@mimosa 但是这样写,在页面好像不能使用分页了?

#6 楼 @yu7272yu kaminari 可以分页。

@mimosa

<nav class="com-pager">
<%= paginate statistical_charging_piles %>   
</nav>

像我在控制器中写的语句,分页就不行

statistical_charging_piles 是什么,方法?

def index_data
        qc = params[:q]
        hs = {"gt" => ">", "gteq" => ">=", "lt" => "<", "lteq" => "<="}
        constr1 = "order_total_sum #{hs[params[:order_total]]} #{params[:order_total_cont]} and " if params[:order_total].present? and params[:order_total_cont].present?
        constr2 = "electricity_total_sum #{hs[params[:electricity_total]]} #{params[:electricity_total_cont]} and " if params[:electricity_total].present? and params[:electricity_total_cont].present?
        cs = "#{constr1} #{constr2} 1=1"
    @q = StatisticalChargingPile.search(qc).result.select("sum(order_total) as order_total_sum, sum(electricity_total) as electricity_total_sum, parking_area_name,charging_pile_id, third_party_pile_id, serial_number, operator_name").group("serial_number").having(cs).order("created_at desc")

        @statistical_charging_piles = @q.result.page(params[:page])

        render :json => {
        :htmls => render_to_string({
            :partial => "index_data",
            :locals => {statistical_charging_piles: @statistical_charging_piles}
        })
    }



end
需要 登录 后方可回复, 如果你还没有账号请 注册新账号