0

我的任务是列出学生的详细信息。我想根据姓名和城市进行搜索。如果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}
4

2 回答 2

1

您的情况应如下所示:

 if( Student.where(:cityId => params[:cityId]).count == 0 )

您拥有的 if 语句测试 aActiveRecord::Relation和 Integer 的相等性,这永远不会是真的

于 2012-12-07T09:43:18.533 回答
0
if User.where(city_id: params[:cityId]).empty?
于 2012-12-07T10:14:35.730 回答