Skip to content

Instantly share code, notes, and snippets.

@spkprav
Created October 8, 2015 11:30
Show Gist options
  • Save spkprav/a7880231f4e33f780bb7 to your computer and use it in GitHub Desktop.
Save spkprav/a7880231f4e33f780bb7 to your computer and use it in GitHub Desktop.
Residence search hack
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