Search in different models depending on condition [ Ruby on Rails ] -


i new in web development , ror in particular, sorry if question obvious. anyway, trying create real estate web site, see takes it, , got stuck on searching. have few models, every each kind of real estate, apartment, office, garage , etc, , when user searches apartment, should use "apartment" model search in, if searches garage, should search in "garage" model , on. so, me , give me hint put in controller?

define mapping user's input classes.

search_types = { 'apartment' => flat, 'garage' => garage, 'house' => house } 

determine class user's input , use finder methods do.

def search   if klass = search_types[params[:search][:type]]     klass.where(params[:search])   else     # handle no matching class type of search   end end 

what not do: try class directly params.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -