module TaxonomyTermsHelper def search_term(params) taxonomy_terms = [] # loop thru each parameter that matches one of the method signatures taxonomy_terms = params.keys.select do |pk| TaxonomyTerm.respond_to?("#{pk}_is".to_sym) or DigitalAsset.respond_to?("#{pk}_in".to_sym) end.reduce(TaxonomyTerm) do |sum, key| # for each key, call the 'named query' method with the value given and chain... method = TaxonomyTerm.respond_to?("#{key}_in".to_sym) ? "#{key}_in".to_sym : "#{key}_is".to_sym sum.send(method, method.to_s.end_with?('in') ? params[key].to_a : params[key]) # should return result of the send call for chaining end taxonomy_terms end end