Skip to content

Instantly share code, notes, and snippets.

@damian
Created April 9, 2011 13:08
Show Gist options
  • Save damian/911387 to your computer and use it in GitHub Desktop.
Save damian/911387 to your computer and use it in GitHub Desktop.
# Required for meta_search to use a named scope
search_methods :for_location, :at_location, :name_contains
scope :for_location, lambda { |location| location = Location.find(location) unless location.is_a?(Location); joins('LEFT OUTER JOIN administrators_locations ON administrators.id = administrator_id').where('location_id IN(?)', location.path_ids) }
scope :at_location, lambda { |location| location = Location.find(location) unless location.is_a?(Location); joins('INNER JOIN administrators_locations ON administrators.id = administrator_id').where('location_id = ?', location.id) }
scope :name_contains, lambda { |name| where("CONCAT_WS(' ', forename, surname) LIKE ?", "%#{name}%") }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment