views:

47

answers:

1

I'm on rails 2.3.5

    puts params[:_search]
    users = User.find(:all) do
    if params[:_search] == "true"
      puts "TESTTTTTTTTTTTTTT"
      pseudo    =~ "%#{params[:pseudo]}%" if params[:pseudo].present?
      firstname =~ "%#{params[:firstname]}%" if params[:firstname].present?
      lastname  =~ "%#{params[:lastname]}%" if params[:lastname].present?                
      email     =~ "%#{params[:email]}%" if params[:email].present?
      role      =~ "%#{params[:role]}%" if params[:role].present?        
    end
    paginate :page => params[:page], :per_page => params[:rows]      
    order_by "#{params[:sidx]} #{params[:sord]}"
  end

This code always shows that it is doing the following query in server logs:

select * from users

even when the :_search parm is true.

What is this code supposed to do? Append where conditions depending on the passed in search condition?

PS: I'm using rails plugin for jqgrid

A: 

Well it's sort of interesting, but all the assignments to psuedo, lastname, email, role are being ignored.

The formatting was slightly off above, it should be

users = User.find(:all) do
  if params[:_search] == "true"
    puts "TESTTTTTTTTTTTTTT"
    pseudo    =~ "%#{params[:pseudo]}%" if params[:pseudo].present?
    firstname =~ "%#{params[:firstname]}%" if params[:firstname].present?
    lastname  =~ "%#{params[:lastname]}%" if params[:lastname].present?                
    email     =~ "%#{params[:email]}%" if params[:email].present?
    role      =~ "%#{params[:role]}%" if params[:role].present?        
    end
  paginate :page => params[:page], :per_page => params[:rows]      
  order_by "#{params[:sidx]} #{params[:sord]}"
end

So, the users = User.find(:all) do is a builder, and taking all of the rest of the code to end as params.

It's pretty non-standard rails code, I think it's trying to build conditions if certain params are sent in. Instead, maybe try this condition builder

User.paginate(:page=>params[:page], :conditions=>build_conditions(params))
Jesse Wolgamott
yes condition builder..that's what I was looking for. I'll read up on that. But what did you change in the code? What formatting was off?
samwick
@samwick -- in your example the "users =" and "if params" were left-aligned. My first time reading it I did not realize that the if was under the User.find
Jesse Wolgamott