Skip to content

Instantly share code, notes, and snippets.

@sslotsky
Created March 22, 2013 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sslotsky/5222683 to your computer and use it in GitHub Desktop.
Save sslotsky/5222683 to your computer and use it in GitHub Desktop.
Use search text to search on a concatenation of db fields.
def self.text_search text = ''
scope = self.scoped
if text.squish!.present?
conditions = []
conditions << sanitize_sql_array(["surveys_large_clinic_individual_compensation_answers.physician_id ILIKE ?", "%#{text}%"])
conditions << sanitize_sql_array(["surveys_large_clinic_individual_compensation_answers.specialty_name ILIKE ?", "%#{text}%"])
conditions << sanitize_sql_array(["org_search.name ILIKE ?", "%#{text}%"])
concat_string = "(surveys_large_clinic_individual_compensation_answers.organization_id || '-'" +
" || surveys_large_clinic_individual_compensation_answers.specialty_id || '-'" +
" || surveys_large_clinic_individual_compensation_answers.physician_id) ILIKE ?"
conditions << sanitize_sql_array([concat_string, "%#{text}%"])
search_joins = "
inner join
organizations org_search
on
#{self.quoted_table_name}.organization_id = org_search.id"
scope = scope.joins(search_joins).where(conditions.join(" OR "))
end
scope
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment