ruby on rails 中的 SQL Like 运算符 [英] SQL Like operator in ruby on rails

查看:37
本文介绍了ruby on rails 中的 SQL Like 运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个任务来选择搜索那些名字以参数值开头的学生,以及所选值中的城市.如何在 ruby​​ on rails 中进行设置?我确实喜欢这个,但这不起作用控制器

I had got a task to select search the students those name start with param value and city in the selected value.How can i set in ruby on rails? i did like this but this is not working controller

 def list

    studentcount=Student.count()
    puts studentcount
    @studentname = Student.where("name name1 AND city = :cityId1",
    {:name1 => params[:name], :cityId1 => params[:cityId]})

    puts 'studentname'
    puts @studentname.inspect
    @students = Student.limit(params[:jtPageSize]).offset(params[:jtStartIndex]).order(params[:jtSorting])

    @jtable = {'Result' => 'OK','Records' => @students.map(&:attributes), :TotalRecordCount => studentcount}

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @jtable}
    end
  end

推荐答案

尝试:

@studentname = Student.where("name LIKE :name1 AND city = :cityId1",
  {:name1 => "#{params[:name]}%", :cityId1 => params[:cityId]})

这是一个相当肮脏的解决方案,但纯 ARel 无法按照您希望的方式处理这种情况.您可能想尝试 Sqeel gem.

This is a rather dirty solution, but pure ARel cannot handle this case the way you desire. You might want to try the Sqeel gem.

这篇关于ruby on rails 中的 SQL Like 运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆