# Using conditions appropriately

**URL:** <https://www.ruby-forum.com/t/using-conditions-appropriately/108670>\
**Category:** Rails\
**Created:** [July 5, 2007, 9:50pm UTC](https://www.ruby-forum.com/t/using-conditions-appropriately/108670 "2007-07-05T21:50:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [July 5, 2007, 9:50pm UTC](https://www.ruby-forum.com/t/using-conditions-appropriately/108670/1 "2007-07-05T21:50:38Z")

</div>

I’ve been struggling with this function for most of the morning, but  
something just doesn’t seem right in my logic with respect to  
efficiency.

I have a basic view that is submitting information on three variables  
directly to this function. The information is accessible simply through  
params[:filtera1], params[:filtera2] and params[:filtera1].

I’m trying to build a find that will check the filter\_a column in my  
database table (places) and list the record if filter\_a matches any one  
of the three parameters just passed to it.

The difficulty comes from two areas

1. I have not perfected the right syntax for using OR in conditions and  
most of the documentation I have found is not too helpful.

2. There is the chance that some of the information passed through may  
be equal to nil - and finds like to break at that point.

Any help would be appreciated.

PS: If anyone is looking for a small job of helping me figure out these  
basic concepts in exchange for some weekly drinking money, I’d be more  
than happy to consider such an arrangement. Everyone’s time is valuable,  
afterall.

---

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [July 5, 2007, 10:05pm UTC](https://www.ruby-forum.com/t/using-conditions-appropriately/108670/2 "2007-07-05T22:05:15Z")

</div>

Why not post your code so we can take a look at the logic?

Regards,  
Dave

* * *

Information and Educational Technology  
Kwantlen University College - 604-599-2120

Robert S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent by: removed\_email\_address@domain.invalid  
05-07-2007 12:50 PM  
Please respond to  
removed\_email\_address@domain.invalid

To  
removed\_email\_address@domain.invalid  
cc

Subject  
[Rails] Using conditions appropriately

I’ve been struggling with this function for most of the morning, but  
something just doesn’t seem right in my logic with respect to  
efficiency.

I have a basic view that is submitting information on three variables  
directly to this function. The information is accessible simply through  
params[:filtera1], params[:filtera2] and params[:filtera1].

I’m trying to build a find that will check the filter\_a column in my  
database table (places) and list the record if filter\_a matches any one  
of the three parameters just passed to it.

The difficulty comes from two areas

1. I have not perfected the right syntax for using OR in conditions and  
most of the documentation I have found is not too helpful.

2. There is the chance that some of the information passed through may  
be equal to nil - and finds like to break at that point.

Any help would be appreciated.

PS: If anyone is looking for a small job of helping me figure out these  
basic concepts in exchange for some weekly drinking money, I’d be more  
than happy to consider such an arrangement. Everyone’s time is valuable,  
afterall.

–  
Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [July 5, 2007, 10:50pm UTC](https://www.ruby-forum.com/t/using-conditions-appropriately/108670/3 "2007-07-05T22:50:25Z")

</div>

On 7/5/07, Robert S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the basic tests, this works fine and passes the data along to the  
> %\>  
> params[:filtera1] OR :filter\_a =\> params[:filtera2] OR :filter\_a =\>  
> params[:filtera3]})
> 
> end

use something like the following:

def results  
conditions = “true”  
par = []

```
unless params[:filtera1].blank?
  conditions << " and filtera = ?"
  par << params[:filtera1]
end

unless params[:filtera2].blank?
  conditions << " or filtera = ?"
  par << params[:filtera2]
end

unless params[:filtera3].blank?
  conditions << " or filtera = ?"
  par << params[:filtera3]
end

@results = Place.find(:all, :conditions => [conditions] + par)

```

end

or use a plugin such as criteria\_query  
([http://www.muermann.org/ruby/criteria\_query/](http://www.muermann.org/ruby/criteria_query/))

Mike

---

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [July 5, 2007, 10:16pm UTC](https://www.ruby-forum.com/t/using-conditions-appropriately/108670/4 "2007-07-05T22:16:29Z")

</div>

Table: Places  
name - location name  
filtera - integer corresponding to a particular area (1, 2 or 3)

View:  
Allows user to select what area they are located in. Checkboxes and Ajax  
were utilized to make the results table dynamically update as they make  
selections, and also to enable users to make multiple selections. From  
the basic tests, this works fine and passes the data along to the  
results function.

\<%= observe\_form 'interest', :frequency =\> 0.25, :update =\> 'search\_target', :url =\> { :controller =\> 'stores', :action =\> 'results' } %\>

Controller:

def results

@results = Place.find(:all, :conditions =\> { :filter\_a =\>  
params[:filtera1] OR :filter\_a =\> params[:filtera2] OR :filter\_a =\>  
params[:filtera3]})

end
