Nit*_*ths 9 ruby ruby-on-rails-3.2
我有一个任务是选择搜索学生那些名字以param值和所选值的城市开头.我怎么能在轨道上设置ruby?我确实喜欢这个,但这不是控制器
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
Run Code Online (Sandbox Code Playgroud)
Chr*_*nig 10
尝试:
@studentname = Student.where("name LIKE :name1 AND city = :cityId1",
{:name1 => "#{params[:name]}%", :cityId1 => params[:cityId]})
Run Code Online (Sandbox Code Playgroud)
这是一个相当肮脏的解决方案,但纯粹的ARel无法按照您的方式处理这种情况.您可能想尝试Sqeel 宝石.
尝试
@studentname = Student.where("name LIKE ? AND city = ?", "#{params[:name]}%", params[:cityId])
Run Code Online (Sandbox Code Playgroud)