Re: 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 –

On Fri, 13 Oct 2006, Ben wrote:

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

If you’re talking about blank?, it’s defined in Rails but not in Ruby,
so in a normal irb session it won’t be available.

David


David A. Black | [email protected]
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

On 10/13/06, Ben D. [email protected] wrote:

for speed. (Careful of this if you use a function as a conditional and rely

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

Ben

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=[]>]>

On 10/13/06, Ben D. [email protected] wrote:

for speed. (Careful of this if you use a function as a conditional and rely

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

Ben

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

Fantastic!

I’ll use a console next time ;^)

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

On 10/13/06, Dark A. [email protected] wrote:

If(params[:category_id] || “”).empty

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


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs