I have a form, with 3 fields, then in my controller I get the paramters to run a query but I don''t want to filter with paramters if they are nil or blank... I''m doing this @condition = '''' if(params.....) @condition = @condition + " myparamter = " + params[..... if(params.....) @condition = @condition + " myparamter = " + params[..... if(params.....) @condition = @condition + " myparamter = " + params[..... but there is a best way to do this? more simple and more beautiful ? tks
"Fernando" <fernando@setti.com.br> wrote in message news:44BCBECB.40701@setti.com.br...>I have a form, with 3 fields, then in my controller I get the paramters to >run a query but I don''t want to filter with paramters if they are nil or >blank... > > I''m doing this > @condition = '''' > if(params.....) > @condition = @condition + " myparamter = " + params[..... > > if(params.....) > @condition = @condition + " myparamter = " + params[..... > > if(params.....) > @condition = @condition + " myparamter = " + params[..... > > but there is a best way to do this? more simple and more beautiful ? > > tks[:param1, :param2, :param3].each do |p_name| @condition += " myparameter=#{params[p_name]}" if params[p_name] && !params[p_name].empty? end needs polishing but that should give you some ideas
Fernando wrote:> I have a form, with 3 fields, then in my controller I get the paramters > to run a query but I don''t want to filter with paramters if they are > nil or blank... > > I''m doing this > @condition = '''' > if(params.....) > @condition = @condition + " myparamter = " + params[..... > > if(params.....) > @condition = @condition + " myparamter = " + params[..... > > if(params.....) > @condition = @condition + " myparamter = " + params[..... > > but there is a best way to do this? more simple and more beautiful ? > > tksDavid A. Black''s book Ruby For Rails is pretty good at showing you cool stuff. Throughout the book he uses this syntax: @condition += params[:bleh] || "this :bleh is :blehnk" the || means if params[:bleh] is nil then use "this :bleh is :blehnk" the += means whatever is in condition now, put the following after it. easier than doing @condition = @condition + ... just do @condition += ... if you wanted to you could do it in 1 line: @condition = "my parameter = " + (params[:bleh] || "this :bleh is :blehnk") + "my parameter2 = " + params[:bleh2] || "this :bleh2 is :blehnk2" etc. -Ben Lisbakken -- Posted via http://www.ruby-forum.com/.
> > David A. Black''s book Ruby For Rails is pretty good at showing you cool > stuff. Throughout the book he uses this syntax: > > @condition += params[:bleh] || "this :bleh is :blehnk" > the || means if params[:bleh] is nil then use "this :bleh is :blehnk" > the += means whatever is in condition now, put the following after it. > easier than doing @condition = @condition + ... just do @condition += > ... > > if you wanted to you could do it in 1 line: > @condition = "my parameter = " + (params[:bleh] || "this :bleh is > :blehnk") + "my parameter2 = " + params[:bleh2] || "this :bleh2 is > :blehnk2" etc. > > -Ben Lisbakkenforgot parenthesis on (params[:bleh2] || "this :bleh2 is :blehnk2") -- Posted via http://www.ruby-forum.com/.
Since blank? is a rails mixin that is true for empty and nil, I believe this will work: [:param1, :param2, :param3].each do |p_name| @condition += " myparameter=#{params[p_name]}" unless params [p_name].blank? end http://jlaine.net/articles/2006/06/14/hidden-rails-goodies-blank - dan -- Dan Kohn <mailto:dan@dankohn.com> <http://www.dankohn.com/> <tel:+1-415-233-1000>
Fernando wrote:> I have a form, with 3 fields, then in my controller I get the paramters > to run a query but I don''t want to filter with paramters if they are > nil or blank... > > I''m doing this > @condition = '''' > if(params.....) > @condition = @condition + " myparamter = " + params[..... > > if(params.....) > @condition = @condition + " myparamter = " + params[..... > > if(params.....) > @condition = @condition + " myparamter = " + params[..... > > but there is a best way to do this? more simple and more beautiful ?Before worrying about simplicity and beauty, worry about correctness and safety. For correctness, you will need to put "AND" between the conditions you are joining (and I''m sure they won''t all use the same "myparameter"). For safety, you should not splice the value from params straight into the conditions string. You should use the form of conditions with "?" placeholders, and supply the arguments separately. Rails will then ensure that the arguments are properly escaped before putting them into the SQL. http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000860 There has been some discussion of building conditions from multiple parameters before in this list, and Ezra Zygmuntowicz produced the ez_where plugin to make building such queries easy. http://brainspl.at/articles/2006/06/30/new-release-of-ez_where-plugin regards Justin