# Want to combine two searches into one

**URL:** <https://www.ruby-forum.com/t/want-to-combine-two-searches-into-one/182563>\
**Category:** Rails\
**Created:** [January 29, 2010, 10:16pm UTC](https://www.ruby-forum.com/t/want-to-combine-two-searches-into-one/182563 "2010-01-29T22:16:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [January 29, 2010, 10:16pm UTC](https://www.ruby-forum.com/t/want-to-combine-two-searches-into-one/182563/1 "2010-01-29T22:16:48Z")

</div>

I would like to merge this method from a controller into a separate  
model, called search.

class DudesController \< ApplicationController  
def search  
@address = params[:address]  
@within = params[:within]

```
@dudes = Restaurant.find :all,
                   :origin => @address,
                   :within => @within,
                   :order => 'distance'

respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @restaurants }
end

```

end

This is the search model:

class Search \< ActiveRecord::Base  
acts\_as\_mappable :auto\_geocode =\> true  
validates\_numericality\_of :minimum\_age, :maximum\_age, :allow\_nil =\> true

def dudes  
@dudes ||= find\_dudes  
end

private

def find\_dudes  
Dude.find(:all, :conditions =\> conditions)  
end

def minimum\_age\_conditions  
[“dudes.birthdate \<= ?”, Date.today - minimum\_age.years] unless  
minimum\_age.blank?  
end

def maximum\_age\_conditions  
[“dudes.birthdate \>= ?”, Date.tomorrow - (maximum\_age+1).years]  
unless maximum\_age.blank?  
end

def category\_conditions  
[“dudes.category\_id = ?”, category\_id] unless category\_id.blank?  
end

def conditions  
[conditions\_clauses.join(’ AND '), \*conditions\_options]  
end

def conditions\_clauses  
conditions\_parts.map { |condition| condition.first }  
end

def conditions\_options  
conditions\_parts.map { |condition| condition[1…-1] }.flatten  
end

def conditions\_parts  
private\_methods(false).grep(/\_conditions$/).map { |m| send(m)  
}.compact  
end

end

I assume I would write something like

def location\_conditions

end

I don’t know how to get the dude.address into a location\_conditions  
method. The search controller is a simple scaffold with the search  
using the create method.
