I need to add conditions depending on params data.
@users = User.where('id', params[:id]) unless params[:id].nil?
@users = User.where('email', params[:email]) unless params[:email].nil?
@users = User.limit(10)
But it does not work for some reason.
Thanks
Each of your statements is replacing the
@usersvariable, and as ActiveRecord evaluates each lazily, the first two are never being called.If you want to maintain three separate queries and build things up that way you can do:
It isn’t the prettiest, but it will work. However, I recommend keeping it to a single method call and defining it in the model.
That way you can use the method again, refine it, and write speed(ier) tests against it.