Friday, January 29, 2010

[Rails] want to combine two searches into one

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.
--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate