SQL As an operator in ruby ​​on rails

I had the task of choosing a search for students whose name begins with the value of the parameter and the city in the selected value. How can I install a ruby ​​on rails? I liked it, but it does not work the 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 
+8
source share
3 answers

Try:

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

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

+10
source share

Try

 @studentname = Student.where("name LIKE ? AND city = ?", "#{params[:name]}%", params[:cityId]) 
+2
source share

there is an error in your methods, it should be like

  @studentname = Student.where("name = :name1 AND city = :cityId1", {:name1 => params[:name], :cityId1 => params[:cityId]}) 
0
source share

All Articles