rails find(:all) do... how does it work?
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 p开发者_开发百科arams[: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
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))
精彩评论