我的任务是列出学生的详细信息。我想根据名称和城市进行搜索。如果是cityId==0 and name=''
,那么我想列出我所有学生的详细信息。我怎样才能做到这一点?我以错误的方式做到了这一点。控制器是:
if(Student.where(params[:cityId])==0)
studentcount = Student.count()
@students = Student.limit(params[:jtPageSize]).offset(params[:jtStartIndex]).order(params[:jtSorting])
@jtable = {'Result' => 'OK','Records' => @students.map(&:attributes), :TotalRecordCount => studentcount}
else
studentcount = Student.where("name LIKE ? AND city = ?", "%#{params[:name]}%", params[:cityId]).count()
@students = Student.where("name LIKE ? AND city = ?", "%#{params[:name]}%", params[:cityId]).limit(params[:jtPageSize]).offset(params[:jtStartIndex]).order(params[:jtSorting])
@jtable = {'Result' => 'OK','Records' => @students.map(&:attributes), :TotalRecordCount => studentcount}
答案 0 :(得分:1)
你的情况应该是这样的:
if( Student.where(:cityId => params[:cityId]).count == 0 )
if-statement你测试了一个ActiveRecord::Relation
和一个Integer的相等,它永远不会是真的
答案 1 :(得分:0)
if User.where(city_id: params[:cityId]).empty?