Created
March 22, 2013 16:26
-
-
Save sslotsky/5222683 to your computer and use it in GitHub Desktop.
Use search text to search on a concatenation of db fields.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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