# A best way to write this

**URL:** <https://www.ruby-forum.com/t/a-best-way-to-write-this/98821>\
**Category:** Rails\
**Created:** [July 18, 2006, 12:58pm UTC](https://www.ruby-forum.com/t/a-best-way-to-write-this/98821 "2006-07-18T12:58:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fernando](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/779978/32.png) [@fernando](https://www.ruby-forum.com/u/fernando)\
**Post date:** [July 18, 2006, 12:58pm UTC](https://www.ruby-forum.com/t/a-best-way-to-write-this/98821/1 "2006-07-18T12:58:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fernando](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/779978/32.png) [@fernando](https://www.ruby-forum.com/u/fernando)\
**Post date:** [July 18, 2006, 2:03pm UTC](https://www.ruby-forum.com/t/a-best-way-to-write-this/98821/2 "2006-07-18T14:03:57Z")

</div>

“Fernando” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in  
message news:removed\_email\_address@domain.invalid…

> @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

---

<div class="post-metadata">

**Author:** ![fernando](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/779978/32.png) [@fernando](https://www.ruby-forum.com/u/fernando)\
**Post date:** [July 18, 2006, 2:25pm UTC](https://www.ruby-forum.com/t/a-best-way-to-write-this/98821/3 "2006-07-18T14:25:04Z")

</div>

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 ?
> 
> tks

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 L.

---

<div class="post-metadata">

**Author:** ![fernando](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/779978/32.png) [@fernando](https://www.ruby-forum.com/u/fernando)\
**Post date:** [July 18, 2006, 5:39pm UTC](https://www.ruby-forum.com/t/a-best-way-to-write-this/98821/4 "2006-07-18T17:39:08Z")

</div>

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](http://jlaine.net/articles/2006/06/14/hidden-rails-goodies-blank)

```
        - dan

```

–  
Dan K. [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.dankohn.com/](http://www.dankohn.com/)tel:+1-415-233-1000

---

<div class="post-metadata">

**Author:** ![fernando](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/779978/32.png) [@fernando](https://www.ruby-forum.com/u/fernando)\
**Post date:** [July 18, 2006, 2:26pm UTC](https://www.ruby-forum.com/t/a-best-way-to-write-this/98821/5 "2006-07-18T14:26:36Z")

</div>

> 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 L.

forgot parenthesis on (params[:bleh2] || “this :bleh2 is :blehnk2”)

---

<div class="post-metadata">

**Author:** ![fernando](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/779978/32.png) [@fernando](https://www.ruby-forum.com/u/fernando)\
**Post date:** [July 19, 2006, 12:59am UTC](https://www.ruby-forum.com/t/a-best-way-to-write-this/98821/6 "2006-07-19T00:59:33Z")

</div>

Fernando wrote:

> @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](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 Z. produced the  
ez\_where plugin to make building such queries easy.

[http://brainspl.at/articles/2006/06/30/new-release-of-ez\_where-plugin](http://brainspl.at/articles/2006/06/30/new-release-of-ez_where-plugin)

regards

Justin
