2012-12-07 11 views
8

Avevo un compito per selezionare cercare gli studenti il ​​nome inizia con valore param e città nel valore selezionato. Come posso impostare in ruby ​​su rotaie? ho fatto come questo, ma questo non sta funzionando regolatoreSQL Come operatore in ruby ​​su rotaie

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 

risposta

10

Prova:

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

Questa è una soluzione piuttosto sporca, ma ARel puro non può gestire questo caso nel modo desiderato. Potresti provare la gemma Sqeel.

+0

grazie per il supporto. – Niths

+0

Puoi aiutarmi a scrivere se l'istruzione per il cityid == 0 – Niths

2

Prova

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

Ciò assicurerà che il nome * termini * con il valore, non inizi con esso. – sevenseacat

+0

Grazie ho modificato la risposta. –

0

c'è un errore nei metodi, dovrebbe essere come questo

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