Created
October 8, 2015 11:30
-
-
Save spkprav/a7880231f4e33f780bb7 to your computer and use it in GitHub Desktop.
Residence search hack
This file contains hidden or 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
class Residency < ActiveRecord::Base | |
scope :filter, lambda { |query, *attributes| | |
return if query.blank? | |
attributes = self.new.attributes.keys if attributes.empty? | |
query.split(/\s/).inject(self) do |collection, phrase| | |
conditions = [""] | |
attributes.each do |attribute| | |
conditions[0] << attribute.to_s << " ILIKE ? OR " | |
conditions.push "%#{phrase}%" | |
end | |
conditions[0] = conditions[0].sub(/ OR $/,"") | |
collection = collection.where(conditions) | |
end | |
} | |
end | |
Residencies.joins(rooms: [:access_point, :building]).filter("Huxton Avenue 203A", | |
'rooms.room_no', | |
'access_pints.app_no', | |
'buildings.name' | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment