patternrubyrailsMinor
Filtering (search) a model with multiple params in hash
Viewed 0 times
paramssearchwithhashmultiplemodelfiltering
Problem
This function in my
Just a note: we're storing in the model some strings containing multiple data, each of these fields should be a one-to-many relationship (db normality violation). I give an example:
I think I can use more
The boss, when introducing the project, said one-to-many relationships for that kind of fields would be a
Profile model receive a hash from a search form and should filter the table depending on those values. I guess there is something like a pattern for such a standard behavior, right now I ended up with this (unfortunately I had to reproduce the code here, so it might not work for some reasons, but you get the idea).Just a note: we're storing in the model some strings containing multiple data, each of these fields should be a one-to-many relationship (db normality violation). I give an example:
country_preferences is a string filled up by an html select :multiple => true. In the string I then find values like: ["Australia", "China", "United Kingdom"].def search opts # This default doesn't work the way I would like = {:user_type => :family}
#TODO: default opts value in method sign if possible
opts[:user_type] ||= :family
# initial criteria
fields= "user_type= ?"
values= [opts[:user_type]]
# each field filters the list in one of these three ways
staight_fields = [ :country, :first_name, :last_name, :nationality ]
like_fields = [:country_preferences, :keyword, :languages ]
bool_fields = [:driver, :housework, :children ]
# cicle the options and build the query
opts.each do |k, v|
if straight_fields.include? k
fields += " AND #{k} = ?"
values += v
elsif like_fields.include? k
fields += " AND #{k} like %?%"
values += v
elsif bool_fields.include? k
fields += " AND #{k} = ?"
values += v == 'true'
else
logger.warn "opts #{k} with value #{v} ignored from search"
end
end
# execute the query and return the result
self.registered.actives.where([fields] + values)
endI think I can use more
scope but how can I combine them together? In general, how can I make this code much better?The boss, when introducing the project, said one-to-many relationships for that kind of fields would be a
Solution
Have you looked into using a pre existing gem for this? I found one called has_scope that seems like it might do exactly what you're trying to refactor. You would need to add a scope for each of your filters and then add some
Here's a few examples. I'm not as familiar with Rails 3 scopes as I am with 2, so the code likely won't work as is.
Model:
Controller:
has_scope calls in your controller.Here's a few examples. I'm not as familiar with Rails 3 scopes as I am with 2, so the code likely won't work as is.
Model:
class Profile country) }
scope :keyword, lambda {|keyword| where(["keyword LIKE :term", {:term => "%#{keyword}%"}]) }
scope :driver, where{:driver => true)
endController:
class ProfileController :boolean
endCode Snippets
class Profile < ActiveRecord::Base
scope :country, lambda {|country| where(:country => country) }
scope :keyword, lambda {|keyword| where(["keyword LIKE :term", {:term => "%#{keyword}%"}]) }
scope :driver, where{:driver => true)
endclass ProfileController < ApplicationController
has_scope :country
has_scope :keyword
has_scope :driver, :type => :boolean
endContext
StackExchange Code Review Q#500, answer score: 6
Revisions (0)
No revisions yet.