Not sure how to code this I''m testing if a param is empty or nil , using - if params[:category_id].empty? I''m just at a loss as to how, if it is nil , omit it from the conditions in the query ? Stuart -- http://en.wikipedia.org/wiki/Dark_ambient --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Did the original on this post come through to the list? Stuart On 10/13/06, Dark Ambient <sambient-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Not sure how to code this > I''m testing if a param is empty or nil , using - if > params[:category_id].empty? > I''m just at a loss as to how, if it is nil , omit it from the conditions > in the query ? > > Stuart > > -- > http://en.wikipedia.org/wiki/Dark_ambient-- http://en.wikipedia.org/wiki/Dark_ambient --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
blank? is useful for all-purpose emptiness testing --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
No, that''ll blow up according to my irb session. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-13 12:38 UTC
Re: testing for nil or empty and then...
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 | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Fantastic! I''ll use a console next time ;^) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On 10/13/06, Ben Dunkley <djtequila-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 >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=[]>]> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On 10/13/06, Ben Dunkley <djtequila-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 >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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 Ambient <sambient-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On 10/13/06, Ben Dunkley <djtequila-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > 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 > > > > 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 >-- http://en.wikipedia.org/wiki/Dark_ambient --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---