# Logic for a search method

**URL:** <https://www.ruby-forum.com/t/logic-for-a-search-method/159724>\
**Category:** Rails\
**Created:** [February 7, 2009, 4:09pm UTC](https://www.ruby-forum.com/t/logic-for-a-search-method/159724 "2009-02-07T16:09:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [February 7, 2009, 4:09pm UTC](https://www.ruby-forum.com/t/logic-for-a-search-method/159724/1 "2009-02-07T16:09:47Z")

</div>

I’m trying to ask users for a name and a place and then give them a list  
of all the names OR all the places OR the names in that place, if you  
get me? This is my flawed attempt, I’m passing params correctly so it’s  
just the logic within the method I need to sort out, anyone mind  
re-factoring it ?

## Here’s my code (wrong)

class School \< ActiveRecord::Base

has\_many :reviews

def self.search(search\_by\_name,search\_by\_place)

```
search_by_name
  find(:all, :conditions => ['name LIKE ?', "%#{search_by_name}%"],

```

:limit =\> 100)  
end

```
search_by_place
  find(:all, :conditions => ['place LIKE ?',

```

“%#{search\_by\_place}%”], :limit =\> 100)  
end

end

end

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [February 7, 2009, 10:09pm UTC](https://www.ruby-forum.com/t/logic-for-a-search-method/159724/2 "2009-02-07T22:09:47Z")

</div>

A couple of things:

1. You want to make sure you protect against SLQ injection, so do not  
pass the variables without escaping them. Rails does this for you when  
it substitutes the ? in the find method.

2. You can use the code below to do what you want.

Hope that helps,

# Alberto.

def self.search(params={})  
conditions = []  
condition\_values = []  
unless params[:name].blank?  
conditions \<\< “name like ?”  
condition\_values \<\< “%#{params[:name]}%”  
end  
unless params[:place].blank?  
conditions \<\< “place like ?”  
condition\_values \<\< “%#{params[:place]}%”  
end  
unless conditions.blank?  
self.find(:all, :conditions =\> [conditions.join(" AND "),  
\*condition\_values], :limit =\> 100)  
else  
[]  
end  
end

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [February 8, 2009, 6:01am UTC](https://www.ruby-forum.com/t/logic-for-a-search-method/159724/3 "2009-02-08T06:01:51Z")

</div>

On Feb 7, 3:09 pm, bingo bob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> has\_many :reviews  
> “%#{search\_by\_place}%”], :limit =\> 100)  
> end
> 
> end
> 
> ## end
> 
> Posted viahttp://www.ruby-forum.com/.

You should take small steps, especially when you are still trying to  
get to grips with the language.

I’ll help you a little by pointing out that this can be a single  
query. What you are trying to do is find stuff that match the  
conditions ‘name LIKE ? OR place LIKE ?’. So this should be a one line  
method.

I would also suggest you look up named\_scope ([railscasts.com](http://railscasts.com) has a  
screencast about it) which you could use here.

Good luck.

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [February 9, 2009, 2:01am UTC](https://www.ruby-forum.com/t/logic-for-a-search-method/159724/4 "2009-02-09T02:01:48Z")

</div>

the params in the Controller is a method that returns the hash coming  
to the request. So you can pass the whole params method in the call.

@schools = School.search(params)

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [February 8, 2009, 9:49am UTC](https://www.ruby-forum.com/t/logic-for-a-search-method/159724/5 "2009-02-08T09:49:01Z")

</div>

Alberto (or anyone)…

How do I pass in the params from the controller to use the method…

Something like

# @schools = School.search(params[:search\_by\_name],

params[:search\_by\_place])

But that gives me …

wrong number of arguments (2 for 1)

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [February 9, 2009, 11:08am UTC](https://www.ruby-forum.com/t/logic-for-a-search-method/159724/6 "2009-02-09T11:08:58Z")

</div>

alberto wrote:

> the params in the Controller is a method that returns the hash coming  
> to the request. So you can pass the whole params method in the call.
> 
> @schools = School.search(params)

Many thanks, after staring at this for while I figured it.

Thanks for your help with this it works great and the idea behind it has  
helped me a great deal.

Seems like this is something that I’ll need to do a lot (pass the whole  
params hash to some method (where it can be dealt with and return  
“stuff”).
