# Specifying none/single/many conditions at the same time

**URL:** <https://www.ruby-forum.com/t/specifying-none-single-many-conditions-at-the-same-time/54740>\
**Category:** Rails\
**Created:** [March 13, 2006, 9:16pm UTC](https://www.ruby-forum.com/t/specifying-none-single-many-conditions-at-the-same-time/54740 "2006-03-13T21:16:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 13, 2006, 9:16pm UTC](https://www.ruby-forum.com/t/specifying-none-single-many-conditions-at-the-same-time/54740/1 "2006-03-13T21:16:21Z")

</div>

Hi!

It’s rather ruby question than rails…  
I have a single ‘list’ action where i call paginate method.  
I can receive none, 1 or 2 variables that specify conditions for  
paginate method.

How to write the code so i can call paginate without conditions if all  
conditions variable are nil, with a single condition if one of  
conditions variables is not empty and with 2 if both are not empty?

For 2 possible variables i could use few if statements, but if i could  
have i.e. 4 or more?

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 14, 2006, 3:44pm UTC](https://www.ruby-forum.com/t/specifying-none-single-many-conditions-at-the-same-time/54740/2 "2006-03-14T15:44:36Z")

</div>

If anyone needed it, here it is (ruby is really simple!):

filters = []  
filters \<\< “column\_name = ‘#{params[:column\_name]}’” unless  
params[:column\_name].nil?

# and so on…

filters = filters.join('and ')  
@properties\_pages, @properties = paginate :property, :order\_by =\> ‘id’,  
:conditions =\> filters, :per\_page =\> 5

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 14, 2006, 8:54pm UTC](https://www.ruby-forum.com/t/specifying-none-single-many-conditions-at-the-same-time/54740/3 "2006-03-14T20:54:13Z")

</div>

On Mar 14, 2006, at 6:44 AM, szymek wrote:

> 

Keep in mind that using it like this is leaving you open to sql  
injection attacks. You need to use the [“column = ?”, params  
[:column]] notation to keep your app safe from sql injection.

Cheers-  
-Ezra Z.  
Yakima Herald-Republic  
WebMaster

> **[yakimaherald.com | Yakima, Washington - We Tell Your Stories](https://www.yakimaherald.com/)**

509-577-7732  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 21, 2006, 8:25pm UTC](https://www.ruby-forum.com/t/specifying-none-single-many-conditions-at-the-same-time/54740/4 "2006-03-21T20:25:56Z")

</div>

Thanks!

Now i know how to delete a table using my own form 🙂

I’m doing another, more complicated (but still simple) form to search  
through the products i keep in my database. I’ve got lots of checkboxes  
that specify type of the product, lower and upper bound of its price and  
text field for specifying additional condition. I’ve put all of them  
into ‘form’ hash.

Is there any way to dynamically create conditions for sql where  
statement using notation used in the previous post based on received  
hash ([“column = ?”, params[:column]])?

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 21, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/specifying-none-single-many-conditions-at-the-same-time/54740/5 "2006-03-21T20:42:11Z")

</div>

On 3/21/06, szymek [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there any way to dynamically create conditions for sql where  
> statement using notation used in the previous post based on received  
> hash ([“column = ?”, params[:column]])?

I can’t tell which message this is a reply to, but I recognize your  
new question. Heh.

I’ve had to solve this problem before, where the only way to get the  
correct result set was to remove certain parameters from the  
:conditions array. However, I didn’t want to just build up the string  
by hand, because then I would need to duplicate the SQL-quoting  
features that ActiveRecord provides.

So, I added a ‘search\_by’ method to the model class. In this case,  
“l2” and “be” are the associated models that I needed to filter on.  
What they represent in the real world isn’t important to the example,  
IMO.

class Plan \< ActiveRecord::Base  
def self.search\_by(l2, be, year, month)  
if l2.nil? && be.nil? # No L2 or BE specified.  
conditions = “”;parameters = []  
elsif be.nil? # L2 specified.  
conditions = "l2\_codes.code = ? and "  
parameters = [l2]  
elsif l2.nil? # BE specified.  
conditions = "budget\_entities.code = ? and "  
parameters = [be]  
else # Both L2 and BE specified.  
conditions = "budget\_entities.code = ? and l2\_codes.code = ? and "  
parameters = [be, l2]  
end  
# Construct the actual query from the conditions and parameters.  
# All ‘list\_plan’ queries specify a year and month.  
query = [conditions + ‘plan\_year = ? and plan\_month = ?’]  
query += parameters + [year, month]  
end  
end

Then, in the controller action that actually needed this functionality:  
def list\_plans  
@page\_title = “Plan List”  
l2, be = prepare\_search\_params(params[:l2], params[:be])  
year = params[:year];month = params[:month]

# Produce a text description for this set of results.

@description = prepare\_description(l2, be)

# Paginate the results, using conditions prepared by the model.

@plan\_pages, @plans = paginate :plans,  
:conditions =\> Plan.search\_by(l2,be,year,month),  
:include =\> [:budget\_entity, :l2\_code]  
if @plans.empty?  
flash[:notice] = “No Plans found with those criteria.”  
redirect\_to :action =\> ‘search’  
end  
end

This was also an attempt to fit this kind of fiddly conditional stuff  
into a “Composed Method” pattern, a term coined by Kent Beck for a  
method where every step operates at the same level of detail. I think  
it works OK, and it’s definitely nicer than putting all the if/else  
logic into the controller.

Hopefully that helps, and doesn’t confuse the issue.

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 22, 2006, 6:28pm UTC](https://www.ruby-forum.com/t/specifying-none-single-many-conditions-at-the-same-time/54740/6 "2006-03-22T18:28:48Z")

</div>

Thanks!

I’ll do it similarly.

I just thought that with all this ‘rails magic’ it can be done in more  
universal, ‘magic’ way 🙂

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 22, 2006, 9:21pm UTC](https://www.ruby-forum.com/t/specifying-none-single-many-conditions-at-the-same-time/54740/7 "2006-03-22T21:21:30Z")

</div>

On Mar 22, 2006, at 9:28 AM, szymek wrote:

> Thanks!
> 
> I’ll do it similarly.
> 
> I just thought that with all this ‘rails magic’ it can be done in more  
> universal, ‘magic’ way 🙂
> 
> –

```
This is the exact kind of dynamic query that I built the ez_where

```

plugin for. Have a look here for the magic i think your looking for 😉

[http://brainspl.at/articles/2006/01/30/i-have-been-busy](http://brainspl.at/articles/2006/01/30/i-have-been-busy)

Cheers

-Ezra Z.  
Yakima Herald-Republic  
WebMaster

> **[yakimaherald.com | Yakima, Washington - We Tell Your Stories](https://www.yakimaherald.com/)**

509-577-7732  
removed\_email\_address@domain.invalid
