RE: [Rails] testing for nil or empty and then...

Try

If !params[:category_id] || params[:category_id].empty?

If the first part of the conditional is true, Ruby will
ignore the next for speed. (Careful of this if you use a function as a
conditional and rely on it being run regardless of the outcome)

You could probably do:

If(params[:category_id]

“”).empty

If params[:category_id] is nil, the evaluation of the parens will be “”.

Ben

blank? is useful for all-purpose emptiness testing

No, that'll blow up according to my irb session.

Hi --

Fantastic!

I'll use a console next time ;^)

Not sure where to go at this point.
Trying the above with no success.
I had also tried blank and didn’t help.
Right now, using CriteriaQuery plugin my statements look like this (i removed the if statement as it was giving me a nil error):

pq = Position.query
pq.category_id_in(params[:category_id])
pq.state_id_in(params[:state_id])
pq.term_id_in(params[:term_id])
pq.city_in(params[:city].split(’,’))
pq.title_in(params[:title].split(’,’))
pp pq
@positions = pq.find

Which seems to work on their own (without and if clause)
Here I only chose one field (printout using PP):

#<Criteria::Query:0xab214e8
@join_aliases={“positions”=>“positions”},
@model_class=Position,
@restrictions=
[#<Criteria::In:0xab20cd8
@attribute_name=“category_id”,

@model_class=Position,
@parent=#<Criteria::Query:0xab214e8 ...>,
@query=#<Criteria::Query:0xab214e8 ...>,
@restrictions=[],
@value="3">,

#<Criteria::In:0xab20b88

@attribute_name="state_id",
@model_class=Position,
@parent=#<Criteria::Query:0xab214e8 ...>,
@query=#<Criteria::Query:0xab214e8 ...>,
@restrictions=[],
@value=nil>,

#<Criteria::In:0xab20a68
@attribute_name=“term_id”,
@model_class=Position,
@parent=#<Criteria::Query:0xab214e8 …>,
@query=#<Criteria::Query:0xab214e8 …>,

@restrictions=[],
@value=nil>,

#<Criteria::In:0xab208b8
@attribute_name=“city”,
@model_class=Position,
@parent=#<Criteria::Query:0xab214e8 …>,
@query=#<Criteria::Query:0xab214e8 …>,

@restrictions=[],
@value=[]>,

#<Criteria::In:0xab20720
@attribute_name=“title”,
@model_class=Position,
@parent=#<Criteria::Query:0xab214e8 …>,
@query=#<Criteria::Query:0xab214e8 …>,

@restrictions=[],
@value=[]>]>

Since these did not work I am trying to alter them to better suit the call. Trying
unless(params[:category_id]).empty? pq.category_id_in(params[:category_id])

ArgumentError

in AjaxsearchController#list

wrong number of arguments (1 for 0)

Not sure why the unless part is illegal, or is maybe it’s the assignment after.
Perhaps there is a better way to phrase it. My thinking is to check if a value exists first then make the assignment.

Stuart

Hey guys , woo woo ! I finally got it right.
pq.category_id_in(params[:category_id])unless(params[:category_id]).empty?

Unless …yep!

Thanks for the help
Stuart